NHibernate和版本控制(timestamp)
本文关键字:timestamp 版本控制 NHibernate | 更新日期: 2023-09-27 18:06:52
我得到了这个异常
在System.Convert。ToDateTime(对象值)
在NHibernate.Type.DateTimeType。获取(IDataReader rs, Int32索引)在p:' NHibernate -core'src'NHibernate'Type' datetimettypes .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>
我一直在寻找解决方案,但还没有找到。任何帮助都是感激的。提前感谢!
我希望你正在使用SQL Server,其中timestamp
不是DateTime
值。没有共同点。参见(最新名称为rowversion
) - rowversion (Transact-SQL)
是一种数据类型,它公开数据库中自动生成的唯一二进制数。rowversion通常用作版本冲压表行的机制。存储空间大小为8字节。rowversion数据类型只是一个递增的数字,不保留日期或时间。要记录日期或时间,请使用datetime2数据类型。
我的建议是,像你一样使用这种类型的列(rowversion/timestamp),并结合<version>
属性的nhibernate功率。看到:
- 5.1.7。版本(可选)
- Ayende NHibernate Mapping - Concurrency
有一个映射的片段:
<version name="Timestamp " generated="always"
unsaved-value="null" type="BinaryBlob">
<column name="Version" not-null="false"
sql-type="timestamp"/>
</version>
如果我们需要传递二进制的东西给客户端,我们可以做一些转换成字符串属性,见:
- NHibernate的乐观并发出会话
属性作为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); }
}