问题
I'm trying to assign a version column to my table and I want the version to be generated from the DB (getDate()). My mapping looks like that:
Version(x => x.Version).Index("idx_Version").Generated.Always();
but when I'm trying to insert a row, I'm getting this error:
Cannot insert the value NULL into column 'Version'... column does not allow nulls.
I've also tried a different approach and mapped the column as an regular column like this:
Map(x => x.Version).Index("idx_Version").Generated.Always();
but it's generated nulls on that column...
Any ideas?
回答1:
I guess that you are using MS SQL DB. There are some similar answers:
- Auto Updated DateTime Column - LastUpdated
- automatic update datetimestamp field
Summary: implement the trigger on UPDATE to have your column changed even during the UPDATE (not only INSERT with GetDate())
Or (I would suggest this appraoch) use a rowversion which will do for you exactly what you want.
来源:https://stackoverflow.com/questions/17128170/nhibernate-version-as-datetime-generated-in-db