然后,我使用Fluent NHibernate及其自动映射功能来映射以下简化的POCO类:
public class Foo
{
public virtual int Id { get; set; }
public virtual datetime CreatedDateTime { get; set; }
}默认情况下,CreatedDateTime字段将映射到SQL DateTime。但是,如果我执行一个测试来检查实体是否被正确创建,那么它将失败。这是因为SQL字段的精度不会一直保持到DateTime数据库。我理解这背后的原因是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");
}
}据我所知,这种映射使NHibernate将CreatedDateTime持久化为SQL类型的datetime2,它可以存储.NET DateTime所能存储的全部精度。这很有效,现在测试通过了。
但是,一次传递会导致另一次失败:我的检查模式导出的测试现在失败了,错误如下:
System.ArgumentException : Dialect does not support DbType.DateTime2
Parameter name: typecode堆栈跟踪为:
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 defaultCatalog, String defaultSchema)
at NHibernate.Cfg.Configuration.GenerateSchemaCreationScript(Dialect dialect)
at NHibernate.Tool.hbm2ddl.SchemaExport..ctor(Configuration cfg, IDictionary`2 configProperties)
at NHibernate.Tool.hbm2ddl.SchemaExport..ctor(Configuration cfg)该代码使用NHibernate.Tool.hbm2ddl.SchemaExport对象调用Execute方法。
我使用的是Fluent v1和NHibernate v2.1。
我还尝试将我的DateTime映射到TimeStamp,但由于插入失败,我甚至无法使映射工作,声明如下:
无法在时间戳列中插入显式值。将INSERT与列列表一起使用可排除时间戳列,或在时间戳列中插入DEFAULT。
有没有人知道如何让SchemeExport与datetime2一起工作,或者如何让时间戳映射为datetime属性工作?
发布于 2010-11-27 05:52:04
我可以使用下面的代码实现我的乐观锁定:(使用datetime2)。
注意,我使用了此处的名称(以及数据类型名称的大小写):http://msdn.microsoft.com/en-us/library/system.data.dbtype.aspx "DateTime2“在我的映射代码中(在CustomType下),而不是Sql Server数据类型大小写("datetime2")。我不确定这是否有区别,但我想指出这一点。
流畅映射:
public class DogBreedMap : ClassMap<DogBreed>
{
public DogBreedMap()
{
Id(x => x.DogBreedUUID).GeneratedBy.GuidComb();
OptimisticLock.Version();
Version(x => x.Version)
.Column("MyTimestamp").CustomType("DateTime2");
}
}
public partial class DogBreed
{
public DogBreed()
{
CommonConstructor();
}
private void CommonConstructor()
{
this.Version = DateTime.MinValue; /*I don't think this is necessary*/
}
public virtual Guid? DogBreedUUID { get; set; }
public virtual DateTime Version { get; set; }
}Sql Server列在以下位置创建:
[MyTimestamp] [datetime2](7) NOT NULL我的基本测试可以工作了,我(正确地)收到了这样的异常(当其他人更新了行时)
行被另一个事务更新或删除(或未保存的值映射不正确):DogBreed#abcb c1d-abc4-abc9-abcb-abca01140a27
at NHibernate.Persister.Entity.AbstractEntityPersister.Check(Int32 rows, Object id, Int32 tableNumber, IExpectation expectation, IDbCommand statement)在id、字段、[]、[]、、#、、# NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object # id、sql、会话)在NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id、Object[]字段、Object[] oldFields、对象rowId、Boolean[] includeProperty、Int32 j、对象、对象obj、sql、会话)。在NHibernate.Action.EntityUpdateAction.Execute()处的NHibernate.Engine.ActionQueue.Execute(IExecutable可执行文件)在NHibernate.Engine.ActionQueue.ExecuteActions(IList处的列表)在NHibernate.Engine.ActionQueue.ExecuteActions()处的NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource会话)在NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent事件处)在NHibernate.Transaction.AdoTransaction.Commit()处的NHibernate.Impl.SessionImpl.Flush()处
https://stackoverflow.com/questions/2033630
复制相似问题