【发布时间】:2014-07-26 19:18:13
【问题描述】:
我正在使用 NHibernate 3.3.3.4000 和 SQL Server 2012。
我已经搜索但没有找到一个示例,说明如何使用 SQL Server 时间戳(即行号)列使用按代码映射(啰嗦)来配置乐观并发。
我还使用 NHibernate 的 SchemaExport.Create() 方法来创建 SQL Server 数据库,因此在 NHibernate 创建数据库后,映射必须在 SQL Server 表中产生一个时间戳类型的 RowVersion 列。
我的 DTO 类是这样设置的:
public class TestDto {
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual Byte[] RowVersion { get; set; }
}
在这里,Ayende 解释了如何使用 XML 映射和 SQL Server 时间戳列设置乐观并发。我尝试使用按代码映射(如下所示)来模仿这一点都以某种方式失败了。
http://ayende.com/blog/3946/nhibernate-mapping-concurrency
这是一个乐观并发的按代码映射示例,但它不使用 SQL Server 时间戳列 - 我尝试猜测使其与时间戳列一起工作所需的更改失败了:
http://notherdev.blogspot.com/2012/01/mapping-by-code-concurrency.html
这是我在前两篇文章中尽我所能确定的按代码映射的非工作尝试:
public TestMap() {
Table( "Test" );
DynamicUpdate( true );
Id( x => x.Id, map => {
map.Column( "ID" );
map.Generator( Generators.GuidComb );
} );
Version( x => x.RowVersion, map => {
map.Column( "RowVersion" );
map.Generated( VersionGeneration.Always );
map.UnsavedValue( null );
map.Insert( true );
//map.Type( new TimestampType() ); // Creates a datetime (not null) column.
//map.Type( new BinaryBlobType() ); // Creates a VARBINARY(MAX) (not null) column.
//map.Access( Accessor.Field ); // Causes error: Could not find property nor field 'RowVersion' in class 'SQC.Repository.Dtos.DataGroupDto'
} );
Property( x => x.Name, map => {
map.Column( "Name" );
map.NotNullable( true );
} );
}
当我取消注释此行时,创建的表的 RowVersion 为“日期时间(非空)” - 我需要 SQL Server 时间戳类型。
map.Type( new TimestampType() ); // Creates a datetime (not null) column.
当我取消注释此行时,创建的表的 RowVersion 为“VARBINARY(MAX) (not null)” - 我需要 SQL Server 时间戳类型。
map.Type( new BinaryBlobType() ); // Creates a datetime (not null) column.
当我取消注释此行时,我在运行时收到此错误“在类 'TestDto' 中找不到属性或字段 'RowVersion'”。
map.Access( Accessor.Field );
我希望有人可以解释如何使用按代码映射(啰嗦)来完成这项工作。该项目主要致力于按代码映射,因此 Fluent NHibernate 不是我们的选择。
【问题讨论】:
标签: sql-server nhibernate concurrency