NHibernate and versioning (timestamp)

后端 未结 1 1735
Happy的楠姐
Happy的楠姐 2021-01-21 08:01

I got this exception

at System.Convert.ToDateTime(Object value)
at NHibernate.Type.DateTimeType.Get(IDataReader rs, Int32 index) in p:\\nhibernate

1条回答
  •  离开以前
    2021-01-21 08:23

    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 attribute. See:

    • 5.1.7. version (optional)
    • Ayende NHibernate Mapping - Concurrency

    There is a snippet of the mapping:

    
        
    
    

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

    0 讨论(0)
提交回复
热议问题