NHibernate - Version as DateTime, generated in DB

自作多情 提交于 2019-12-24 19:05:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!