[英]NHibernate read timestamp column from SQL Server
I'm using NHibernate 4.0.0.4000 (with mapping by code) and SQL Server 2012. 我正在使用NHibernate 4.0.0.4000(通过代码映射)和SQL Server 2012。
I have to access tables which (among others) contain a timestamp
(otherwise also known as rowversion) column. 我必须访问其中包含
timestamp
(否则也称为行版本)列的表。 Until now I could simply ignore the column by not using it in my Entity/Mapping. 到现在为止,我可以通过不在实体/映射中使用它来简单地忽略该列。
Now I want to use it to see, which rows have changed since the last time I checked the table in question. 现在,我想使用它来查看自上次检查相关表以来哪些行已更改。
Since timestamp may only be changed by SQL Server, any INSERT
/ UPDATE
statements generated by NHibernate must ignore this column. 由于时间戳只能由SQL Server更改,因此NHibernate生成的任何
INSERT
/ UPDATE
语句都必须忽略此列。 I didn't find a solution for this problem. 我没有找到解决此问题的方法。
My entity: 我的实体:
public class TblAnwendungsbelegposition {
public virtual int ANWBID { get; set; }
// Other properties cut out for readability
public virtual byte[] upsize_ts { get; set; }
}
My mapping: 我的映射:
public class TblAnwendungsbelegpositionMap : ClassMapping<TblAnwendungsbelegposition> {
public TblAnwendungsbelegpositionMap() {
Table("tbl_anwendungsbelegposition");
Schema("dbo");
Lazy(true);
Id(x => x.ANWBID, map => map.Generator(Generators.Identity));
//Other properties cut out for readability
Property(x => x.upsize_ts, map => map.Access(Accessor.ReadOnly));
}
}
Result for this version: 此版本的结果:
Cannot update a timestamp column
无法更新时间戳列
as an error message during reading (on the automatic Session.Flush()
on closing the using-statement wrapping the db connection). 作为读取过程中的错误消息(在自动
Session.Flush()
上,关闭包装数据库连接的using语句时)。
When I remove the accessor in the mapping I can read without problems, but when I insert a new line into the table, I get the message 当我在映射中删除访问器时,我可以毫无问题地阅读,但是当我在表中插入新行时,我得到了消息
Cannot insert an explicit value into a timestamp column.
无法将明确的值插入时间戳列。 Use INSERT with a column list to exclude the timestamp column, or insert a DEFAULT into the timestamp column.
将INSERT与列列表一起使用可排除timestamp列,或将DEFAULT插入timestamp列。
When I change the set;
当我改变
set;
to private set;
private set;
, I get the message during the initial BuildSessionFactory();
,我在初始
BuildSessionFactory();
期间收到了消息BuildSessionFactory();
The following types may not be used as proxies: PigTool.Entities.TblAnwendungsbelegposition: method set_upsize_ts should be 'public/protected virtual' or 'protected internal virtual'
以下类型不能用作代理:PigTool.Entities.TblAnwendungsbelegposition:方法set_upsize_ts应该是“公共/受保护的虚拟”或“受保护的内部虚拟”
When I remove the set;
当我删除
set;
from the entity-property, I get the message during the initial BuildSessionFactory();
从实体属性,我在初始
BuildSessionFactory();
期间收到消息BuildSessionFactory();
Could not find a setter for property 'upsize_ts' in class 'PigTool.Entities.TblAnwendungsbelegposition'
在类“ PigTool.Entities.TblAnwendungsbelegposition”中找不到属性“ upsize_ts”的设置器
Does anyone have an idea how I can successfully read rows containing this readonly column, while retaining the ability to generate new rows? 有谁知道如何在保留生成新行的能力的同时成功读取包含此只读列的行? I'd prefer not to have two entities/mappings per relevant table, where one doesn't contain the timestamp property for everyday work and one which has it, but is only ever used for reading.
我不希望每个相关表都没有两个实体/映射,其中一个不包含日常工作的timestamp属性,而一个拥有它的属性仅用于读取。
I finally found a solution. 我终于找到了解决方案。
In the mapping exchange the 在映射交换中
Property(x => x.upsize_ts, map => map.Access(Accessor.ReadOnly));
with 与
Version(x => x.upsize_ts, map =>
{
map.Column(c =>
{
c.SqlType("timestamp");
c.NotNullable(false);
});
map.Type(new BinaryBlobType());
map.Generated(VersionGeneration.Always);
map.UnsavedValue(null);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.