然后我使用Fluent NHibernate及其自动化功能来映射以下简化的POCO类:
public class Foo
{
public virtual int Id { get; set; }
public virtual datetime CreatedDateTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,CreatedDateTime字段将映射到SQL DateTime.但是,如果我进行测试以检查实体是否正确创建,则会失败.这是因为DateTime字段的精度不会保留到SQL数据库.我强调这背后的原因是MS SQL Server DateTime只能通过四舍五入到.000,.003或.007的增量来保持毫秒精度(请参阅http://msdn.microsoft.com/en-us/library /ms187819.aspx).因此,NHibernate在保存到商店时会截断毫秒数.这导致我的测试失败,当检查正确持久的字段,因为我的.NET DateTime保持其毫秒,但在保存已经失去其毫秒之后重新启动DateTime,因此两者不是真正相等.
为了解决这个问题,我将以下映射添加到Foo对象:
public class FooMap : IAutoMappingOverride<Foo>
{
public void Override(AutoMapping<Foo> mapping)
{
mapping.Map(f => f.CreatedDateTime).CustomType("datetime2");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这个映射使得NHibernate将CreatedDateTime持久化为datetime2的SQL类型,它可以存储.NET DateTime可以的完整精度.这是一种享受,现在测试通过了.
然而,一次传递另一个失败:我检查架构导出的测试现在失败,出现以下错误:
System.ArgumentException : Dialect does not support DbType.DateTime2
Parameter name: typecode
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
at NHibernate.Dialect.TypeNames.Get(DbType typecode)
at NHibernate.Dialect.Dialect.GetTypeName(SqlType sqlType)
at NHibernate.Mapping.Column.GetDialectTypeName(Dialect dialect, IMapping mapping)
at NHibernate.Mapping.Table.SqlCreateString(Dialect dialect, IMapping p, String …Run Code Online (Sandbox Code Playgroud) 我正在使用Fluent Nhibernate和Nhibernate来完成我当前的项目.我需要将时间记录到毫秒.我有这个用于我的映射
Map(x => x.SystemDateTime)
.CustomType("Timestamp")
.Not.Nullable();
Run Code Online (Sandbox Code Playgroud)
我创建了hbm.xml文件,该行如下:
<property name="SystemDateTime" type="Timestamp">
<column name="SystemDateTime" not-null="true" />
</property>
Run Code Online (Sandbox Code Playgroud)
我已经读过这是修复,但数据库中的记录没有毫秒.有谁解决了这个问题.我也尝试过CustomSqlType.
谢谢