【问题标题】:NHibernate read timestamp column from SQL ServerNHibernate 从 SQL Server 读取时间戳列
【发布时间】:2016-09-26 09:33:51
【问题描述】:

我正在使用 NHibernate 4.0.0.4000(通过代码映射)和 SQL Server 2012。

我必须访问(除其他外)包含timestamp(也称为rowversion)列的表。到目前为止,我可以通过不在我的实体/映射中使用它来简单地忽略该列。

现在我想用它来查看自上次检查相关表以来哪些行发生了变化。

由于时间戳只能由 SQL Server 更改,因此 NHibernate 生成的任何 INSERT / UPDATE 语句都必须忽略此列。我没有找到解决这个问题的方法。

我的实体:

public class TblAnwendungsbelegposition {
    public virtual int ANWBID { get; set; }
    // Other properties cut out for readability
    public virtual byte[] upsize_ts { get; set; }
}

我的映射:

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));
    }
}

此版本的结果:

无法更新时间戳列

作为阅读过程中的错误消息(在自动Session.Flush() 关闭包装数据库连接的使用语句时)。

当我删除映射中的访问器时,我可以毫无问题地读取,但是当我在表中插入新行时,我会收到消息

无法将显式值插入时间戳列。将 INSERT 与列列表一起使用以排除时间戳列,或将 DEFAULT 插入时间戳列。

当我将set; 更改为private set; 时,我会在初始BuildSessionFactory(); 期间收到消息

以下类型不能用作代理: PigTool.Entities.TblAnwendungsbelegposition:方法 set_upsize_ts 应该是“公共/受保护的虚拟”或“受保护的内部虚拟”

当我从实体属性中删除 set; 时,我会在初始 BuildSessionFactory(); 期间收到消息

在“PigTool.Entities.TblAnwendungsbelegposition”类中找不到属性“upsize_ts”的设置器

有谁知道我如何成功读取包含此只读列的行,同时保留生成新行的能力?我不希望每个相关表有两个实体/映射,一个不包含用于日常工作的时间戳属性,另一个包含它,但仅用于读取。

【问题讨论】:

    标签: c# sql-server nhibernate nhibernate-mapping-by-code


    【解决方案1】:

    我终于找到了解决办法。

    在映射交换中

            Property(x => x.upsize_ts, map => map.Access(Accessor.ReadOnly));
    

            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);
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 2011-11-21
      • 2014-07-26
      • 2020-09-05
      • 1970-01-01
      • 2011-03-02
      相关资源
      最近更新 更多