NHibernate和版本控制(时间戳) [英] NHibernate and versioning (timestamp)
问题描述
我有这个例外
在System.Convert.ToDateTime(对象值)上
在NHibernate.Type.DateTimeType.Get(IDataReader rs,Int32索引)中位于p:\ nhibernate-core \ src \ NHibernate \ Type \ DateTimeType.cs:line 43
at System.Convert.ToDateTime(Object value)
at NHibernate.Type.DateTimeType.Get(IDataReader rs, Int32 index) in p:\nhibernate-core\src\NHibernate\Type\DateTimeType.cs:line 43
我猜测系统尝试将行版本转换为日期时间时发生了错误
I guess the error occured when system trying to convert rowversion to datetime
这是我获取列表的方法
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>
我已经搜索了一种解决方案,但没有找到解决方案.任何帮助表示赞赏.预先感谢!
I have searched for a solution but haven't found one. Any help is appreciated. Thanks in advance!
推荐答案
我希望您使用的是SQL Server,其中timestamp
不是DateTime
值.没有任何共同之处.请参阅(最新名称为rowversion
)-行版本号(Transact -SQL)
I would expect that you are using SQL Server, where timestamp
is not a DateTime
value. Nothing in common. See (the up-to-date name is rowversion
) - rowversion (Transact-SQL)
是一种数据类型,它公开了数据库中自动生成的唯一二进制数. 行版本通常用作对表行进行版本标记的机制.存储大小为8个字节. 行版本数据类型只是一个递增数字,并不保留日期或时间.要记录日期或时间,请使用 datetime2 数据类型.
Is a data type that exposes automatically generated, unique binary numbers within a database. rowversion is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The rowversion data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime2 data type.
我的建议是,与<version>
属性的NHiberante结合使用,确实像您一样使用这种类型的列(行版本/时间戳).参见:
What I would suggest, do use that type of column (rowversion/timestamp) as you did, with conjunction of NHiberante power of <version>
attribute. See:
- 5.1.7.版本(可选)
- Ayende NHibernate映射-并发
- 5.1.7. version (optional)
- Ayende NHibernate Mapping - Concurrency
有一个映射片段:
<version name="Timestamp " generated="always"
unsaved-value="null" type="BinaryBlob">
<column name="Version" not-null="false"
sql-type="timestamp"/>
</version>
如果我们需要将该二进制内容传递给客户端,我们可以将其转换为字符串属性,请参见:
If we need to pass that binary stuff to client we can do some conversion into string property, see:
作为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); }
}
这篇关于NHibernate和版本控制(时间戳)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!