1

Before posting this question, I've seen a lot of examples on NH Versioning and all of those tells me using below code; I can have a versioned-entity using MappingByCode:

Version(e => e.MyVersion, vm => {
     vm.Generated(VersionGeneration.Always);
     vm.UnsavedValue(0);
});

I expected that assigning MyVersion should be done by SQL Server. So when I call ISession.Save or ISession.SaveOrUpdate; NHProf shows generated sql contains no MyVersion parameter passed to Insert statement (as expected) but query execution goes to be failed because SQL Server doesn't generate it; and result is cannot insert null value into MyVersion column... error.

My expectation about VersionGeneration.Always is correct? so what's wrong?

I'm using NHibernate 3.3 / SQL Server 2012

Sadegh
  • 4,181
  • 9
  • 45
  • 78

1 Answers1

2

VersionGeneration.Always means that NHibernate should expect the database system to generate the version number both on INSERT and on UPDATE.

But you also need to tell SQL Server that it's supposed to generate the number... See for instance http://msdn.microsoft.com/en-us/library/ms182776.aspx.

Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
  • Hm... Actually I'm using SchemaExport for schema generation, so I need to manually make MyVersion to be versionable over SQL Server? It's too bad :D – Sadegh Feb 12 '13 at 16:11
  • 1
    I suspect that it's generating an int column in the database. The idea that the column is auto-updating is orthogonal to the type of the column. Using rowversion is one option, but conceivably a solution based on int + trigger or something else might be used. Therefore you must also tell NHibernate which type to use. I see examples of Byte[] in the domain plus mapping to set sql-type as rowversion. – Oskar Berggren Feb 12 '13 at 16:17
  • 1
    Put another way: generated="always" only states that NHibernate should not generate a value, because the DB will do it, but it does not imply _how_ the value is generated. – Oskar Berggren Feb 12 '13 at 16:18
  • Great link to a `rowversion` replacing obsolete *timestamp*. That name was so confusing... – Radim Köhler Feb 13 '13 at 06:24