如何使用代码映射在 nhibernate 3.2 中设置表列的默认值?
我使用 Fluent NHibernate 作为我的 NHibernate 项目的映射机制。但当谈到 NHibernate 3.2 时,我意识到它内置了代码映射,并且不会为 NHibernate 3.2 发布 Fluent Nhibernate 版本。
当我想为实体属性设置默认值时遇到一个问题,我没有找到执行此操作的 API。有人可以给一些建议吗?
I was using Fluent NHibernate as mapping mechanism for my NHibernate projects. But when it came to NHibernate 3.2, I realized that it has mapping by code built in, and no Fluent Nhibernate releases will be published for NHibernate 3.2.
I encountered a question when I want to set a default value for a entity property, I didn't find the API to do that. Can anybody give some advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有使用代码映射,但是
NHibernate.Mapping.ByCode.Impl.ColumnMapper
类有一个方法Default(object defaultValue)
。 NHibernate JIRA 中的这个问题除了指出一个警告之外,还展示了如何使用它:这是您要找的吗?
I haven't used mapping by code yet, but the
NHibernate.Mapping.ByCode.Impl.ColumnMapper
class has a methodDefault(object defaultValue)
. This issue in NHibernate's JIRA, apart from pointing out a caveat, shows how to use it:Is this what you were looking for?
我找到了这段代码。可以帮助你。
I found this code. May help u.
当首先使用数据库时,在映射中设置默认值不起作用。
您必须告诉 nhibernate 使用数据库默认值。
为此,首先为数据库中的列设置默认值。
之后,您可以通过执行以下操作告诉 nhibernate 映射忽略插入时的列:
When using a DataBase first, setting the default value in the mapping doesn´t work.
You have to tell the nhibernate to work with the DataBase Default value.
To do that, first you set the default value for the column in your Database.
After that, you tell the nhibernate mapping to ignore the column at insert by doing this: