NHibernate and versioning (timestamp)
c#, nhibernate
Solution
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)
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.
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. version (optional)
- Ayende NHibernate Mapping - Concurrency
There is a snippet of the mapping:
<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:
- Optimistic concurrency out of session in NHibernate
property as a C# code:
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); }
}
Problem
I got this exception 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 This is my method to get List ``` public static IList<Employee> getEmployees() { using (ISession mySession = SessionFactory().OpenSession()) { using (ITransaction myTransaction = mySession.BeginTransaction()) { return mySession.CreateCriteria<Employee>().List<Employee>(); } } } ``` This is my class ``` 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; } } ``` This is my mapping ``` <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!