流利的NHibernate生成和分配的ID列 [英] Fluent NHibernate Generated AND Assigned ID Columns

查看:84
本文介绍了流利的NHibernate生成和分配的ID列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fluent NHibernate来实现Web应用程序中的数据持久性.

I'm using Fluent NHibernate for my data-persistence in a web application.

我的问题...我有一个基类,该类使用GeneratedBy().Identity()映射ID类型为T(几乎总是int或GUID)的ID属性的所有实体

My problem... I have a base class that maps all entities with an ID property of type T (almost always an int or GUID) using GeneratedBy().Identity()

在应用程序启动时,我有一个引导程序,用于检查并验证是否填充了所需的种子数据.我的问题是,填充的某些种子数据需要特定的ID. (对应于枚举或系统用户的ID)

On application start-up, I have a boot-strapper that checks and verifies the needed seed-data is populated. My problem is, some of the seed-data that is populated needs a specific ID. (IDs that would correspond to an enum or system user)

是否有任何方法可以强制NHibernate使用 I 指定的ID而不是自动生成的ID来提交记录?此后对存储库的任何其他提交都可以自动生成.

Is there any way to force NHibernate to commit the record using the ID that I specify, rather than an auto-generated one? Any other commits to the repository thereafter can be auto-generated.

推荐答案

Id(x => x.Id).GeneratedBy.Assigned();

如果您希望应用程序分配标识符(而不是让NHibernate生成标识符),则可以使用分配的生成器.这个特殊的生成器将使用已经分配给对象的标识符属性的标识符值.使用此功能分配具有业务含义的键时要非常小心(几乎总是一个糟糕的设计决策).

If you want the application to assign identifiers (as opposed to having NHibernate generate them), you may use the assigned generator. This special generator will use the identifier value already assigned to the object's identifier property. Be very careful when using this feature to assign keys with business meaning (almost always a terrible design decision).

由于其固有的性质,使用此生成器的实体无法通过ISession的SaveOrUpdate()方法保存.相反,您必须通过调用ISessionSave()Update()方法来显式指定NHibernate是保存还是更新对象.

Due to its inherent nature, entities that use this generator cannot be saved via the ISession's SaveOrUpdate() method. Instead you have to explicitly specify to NHibernate if the object should be saved or updated by calling either the Save() or Update() method of the ISession.

http://nhibernate.info/doc/nhibernate-reference/mapping.html#mapping-declaration-id-assigned

这篇关于流利的NHibernate生成和分配的ID列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆