我正在(稍微)深入了解使用 NHibernate 的流畅界面进行自动映射。非常好的东西,但我遇到了 DateTimes 的一个小问题。我需要将数据格式更改为时间戳,否则 NHibernate 会截断毫秒数。
我找到了几个信息来源,最好的一个是: AutoMapping Info 1他正在更改属性的列名和类型。问题是,根据 this document,流畅的自动映射发生了变化。 .
现在我不知道如何让自动映射“改变类型”。我尝试了以下代码,但我被卡住了。同样,我想做的只是告诉自动映射器:
使用 DateTime 的时间戳以防止在使用自动映射时截断毫秒。
有人知道吗?到目前为止的代码:
public class DateTimeToTimestamp : IClassConvention
{
public bool Accept(IClassMap target)
{
return target.GetType() == typeof(DateTime);
}
public void Apply(IClassMap target)
{
throw new NotImplementedException();
}
}
好的, 非常感谢您的回答...那样对我来说已经足够安慰了。如果我真的有 3 个类需要这种精度,我可以处理写 3 次。特别是因为所有其他属性的映射仍然完美,下面的代码只替换了我想要的一个属性......非常好!
如果有人知道更通用的方法,请随意添加,但就目前而言,我很高兴!
我的案例代码是:
public class DateTimeToTimestamp : IAutoMappingOverride<CustomTime>
{
public void Override(AutoMap<CustomTime> mapping)
{
mapping.Map(x => x.ScanDate).CustomTypeIs("timestamp");
}
}
最佳答案
要扩展 Derek 的答案,以便在更一般的层面上进行,您可以使用自动映射约定:
public class TimestampTypeConvention : IPropertyConvention, IPropertyConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Type == typeof (DateTime));
}
public void Apply(IPropertyInstance instance)
{
instance.CustomType<TimestampType>();
}
}
关于c# - Fluent NHibernate 自动映射 : Alter DateTime to Timestamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/894264/