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

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

问题描述

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

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

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

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

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

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