NHibernate和版本控制(时间戳)

时间:2014-06-05 03:20:14

标签: c# nhibernate

我得到了这个例外

  

在System.Convert.ToDateTime(对象值)
    at NHibernate.Type.DateTimeType.Get(IDataReader rs,Int32 index)在p:\ nhibernate-core \ src \ NHibernate \ Type \ DateTimeType.cs:第43行

我猜系统在尝试将rowversion转换为datetime时发生了错误

这是获取List

的方法
public static IList<Employee> getEmployees()
{
    using (ISession mySession = SessionFactory().OpenSession())
    {
        using (ITransaction myTransaction = mySession.BeginTransaction())
        {
            return mySession.CreateCriteria<Employee>().List<Employee>();
        }
    }
}

这是我的班级

public class Employee {
    private int _id;
    private Rank _rank;
    private string _visa;
    private string _firstName;
    private string _lastName;
    private string _university;
    private DateTime _rowversion;
    public Employee() {
        EmployeeFunctionInProject = new List<EmployeeFunctionInProject>();
        Group = new List<Group>();
    }
    public virtual int Id {
        get {
            return this._id;
        }
        set {
            this._id = value;
        }
    }
    public virtual Rank Rank {
        get {
            return this._rank;
        }
        set {
            this._rank = value;
        }
    }
    public virtual string Visa {
        get {
            return this._visa;
        }
        set {
            this._visa = value;
        }
    }
    public virtual string FirstName {
        get {
            return this._firstName;
        }
        set {
            this._firstName = value;
        }
    }
    public virtual string LastName {
        get {
            return this._lastName;
        }
        set {
            this._lastName = value;
        }
    }
    public virtual string University {
        get {
            return this._university;
        }
        set {
            this._university = value;
        }
    }
    public virtual DateTime Rowversion {
        get {
            return this._rowversion;
        }
        set {
            this._rowversion = value;
        }
    }
    public virtual IList<EmployeeFunctionInProject> EmployeeFunctionInProject { get; set; }
    public virtual IList<Group> Group { get; set; }
}

这是我的映射

<hibernate-mapping assembly="MyWeb11" namespace="MyWeb11.Models" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Employee" table="EMPLOYEE" lazy="true" >
    <id name="Id" column="ID">
      <generator class="identity" />
    </id>
    <many-to-one name="Rank">
      <column name="RANK" sql-type="int" not-null="true" />
    </many-to-one>
    <property name="Visa">
      <column name="VISA" sql-type="varchar" not-null="true" unique="true" />
    </property>
    <property name="FirstName">
      <column name="FIRST_NAME" sql-type="varchar" not-null="true" />
    </property>
    <property name="LastName">
      <column name="LAST_NAME" sql-type="varchar" not-null="true" />
    </property>
    <property name="University">
      <column name="UNIVERSITY" sql-type="varchar" not-null="true" />
    </property>
    <property name="Rowversion">
      <column name="ROWVERSION" sql-type="timestamp" not-null="true" />
    </property>
    <bag name="EmployeeFunctionInProject" inverse="true">
      <key column="EMPLOYEE" />
      <one-to-many class="EmployeeFunctionInProject" />
    </bag>
    <bag name="Group" inverse="true">
      <key column="LEADER" />
      <one-to-many class="Group" />
    </bag>
  </class>
</hibernate-mapping>

我已经找到了一个解决方案但尚未找到解决方案。任何帮助表示赞赏。提前谢谢!

1 个答案:

答案 0 :(得分:3)

我希望您使用的是SQL Server,其中timestamp不是DateTime值。没有任何共同之处。请参阅(最新名称为rowversion) - rowversion (Transact-SQL)

  

是一种在数据库中公开自动生成的唯一二进制数的数据类型。 rowversion 通常用作对表行进行版本标记的机制。存储大小为8个字节。 rowversion 数据类型只是一个递增的数字,不会保留日期或时间。要记录日期或时间,请使用 datetime2 数据类型。

我建议使用这种类型的列(rowversion / timestamp),并结合<version>属性的NHiberante功能。参见:

有一个映射片段:

<version name="Timestamp " generated="always"
         unsaved-value="null" type="BinaryBlob">
    <column name="Version" not-null="false"
            sql-type="timestamp"/>
</version>

如果我们需要将二进制内容传递给客户端,我们可以转换为字符串属性,请参阅:

属性作为C#代码:

protected virtual byte[] Timestamp { get; set; }
public virtual string Version
{
    get { return Timestamp.IsEmpty() ? null : Convert.ToBase64String(Timestamp); }
    set { Timestamp = value.IsEmpty() ? null : Convert.FromBase64String(value); }
}