NHibernate +默认的getdate()列 [英] NHibernate + default getdate() column
问题描述
我如何配置NHibernate来创建具有以下列的数据库架构:
How do I configure NHibernate to create the db schema with a column like this:
create_dt datetime not null default getdate()
我在映射文件中有这个
<property name="create_dt" update="false" insert="false" generated="insert" not-null="true" />
无论如何,我可以注入特定于sql server的default getdate()
. 生成的属性的文档甚至提到这是您处理create_date字段的方式.我只是不确定如何使我的数据库模式正确生成.我需要手动编辑创建表脚本吗?
Is there anyway I can inject the sql server specific default getdate()
. The documentation for generated properties even mentions this is how you handle a create_date field. I'm just not sure how to make my db schema generate properly. Will I have to edit the create table scripts manually?
类似的问题.
编辑:我发现我总是可以像这样更改表架构:
EDIT: I figured out I can always change the table schema like so:
<database-object>
<create>ALTER TABLE Report ADD CONSTRAINT DF_report_create_dt DEFAULT getdate() FOR create_dt;</create>
<drop></drop>
</database-object>
,我可以用相同的方式为update_dt类型的字段添加一个触发器.这似乎比提供使用getdate()的显式插入和更新语句更好.
and I could add a trigger in the same way for an update_dt type of field. This seems better than supplying explicit insert and update statements that use getdate().
推荐答案
我总是更喜欢使用NHibernate Event系统来设置我的审核属性,例如创建日期或更新日期. (请参见事件系统文档此处).
I alway prefer to use the NHibernate Event system to set my audit properties like created date or update date. (See event system documentation here).
我更喜欢这种方法,因为它将逻辑排除在数据库层之外,而且还使我能够在代码中拥有一个负责设置这些值的位置.而且,如果我对所有实体都有一个通用的基类,那么我什至可以保证在我的整个域中行为的一致性.
I prefer this approach because it keeps the logic out of my database layer but also it gives me the ability to have a single location in my code that is responsible for setting these values. And if I have a common base class for all my entities then I can even guarantee consistent behavior throughout my domain.
这篇关于NHibernate +默认的getdate()列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!