NHibernate-版本为DateTime,在数据库中生成 [英] NHibernate - Version as DateTime, generated in DB
问题描述
我正在尝试向表分配一个版本列,并且希望从数据库(getDate())生成版本.我的映射如下所示:
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...
有什么想法吗?
推荐答案
我猜您正在使用MS SQL DB.有一些类似的答案:
I guess that you are using MS SQL DB. There are some similar answers:
- Auto Updated DateTime Column - LastUpdated
- automatic update datetimestamp field
摘要:在UPDATE上实现触发器以使您的列即使在UPDATE期间也要更改(不仅是通过GetDate()插入)
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.
这篇关于NHibernate-版本为DateTime,在数据库中生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!