tags:

views:

77

answers:

2

As per the StackOverflow question 'NHibernate and sql timestamp columns as version', I use the following mapping:

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

<property name="CreateDate" column="CreateDate" type="DateTime" update="false" insert="false" />

(Other properties after this last).

But when I run my ASP.MVC app I get:

[Path]\Clients.hbm.xml(7,90): XML validation error: The element 'urn:nhibernate-mapping-2.2:version' cannot contain child element 'urn:nhibernate-mapping-2.2:column' because the parent element's content model is empty.

But as far as I can see 2.2 is the latest version of the mapping, so how can anyone put a column element inside the version element?

Sorry if this is really basic,

A: 

A quick look in the documentation reveals that your mapping is not correct. It should be something like this:

<version name="RowNumber" column="RowNumber"
         generated="always" unsaved-value="null"
         type="Timestamp" />

Best Regards,
Oliver Hanappi

Oliver Hanappi
Thanks for your reply - But this produces:Could not cast the value in field RowNumber0_ of type Byte[] to the Type TimestampType. Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type.As per Ayende's blog, a SQL server rowversion/timestamp needs to be mapped to a BinaryBlob
BruceK
Ah, I see. Have you tried the BinaryBlob data type or Int32 or something similar? I used Timestamp because the NHibernate documentation says that you can use only very few types: Int64, Int32, Int16, Ticks, Timestamp, or TimeSpan.
Oliver Hanappi
A: 

In case anyone else has this problem:

It works as Ayende Rahien specifies in this blog on NHibernate - but only (AFAIK) on version 2.1.n; I was using 2.0.n. I also think you need the object's field/property to be byte[], not System.Linq.Binary as that type has no default constructor (but I am not sure about this - I seemed to have to do this)

Example (excuse the names):

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

A SQL server 'timestamp' is not your regular timestamp, hence the requirement that the type should be a binary blob.

Note that if you do migrate you will need to change the NHibernate configuration in Web/App config - most tutorials currently available seem to be for v.2.0 (or earlier) - you need an uptodate reference or tutorial for 2.1

BruceK