当通过 NHibernate 更新/插入实体而不扩展具有特殊属性的域类时,如何设置一些特殊的列值?
例如:在我的情况下,我想获取对象,并在更新/插入数据库之前使用 IInterceptor 向该对象添加一些附加信息(如用户 ID 或计算机名称)。换句话说,我想在数据库表中添加几列而不在原始对象的类中指定新属性。在这种情况下,我是否必须配置/更改/添加到我的Object.hbm.xml
或 App.config 中?
问题是我无法更改我的原始对象和基类。所以我必须弄清楚是否可以在不更改原始对象的情况下向 DB 表添加信息(甚至没有从任何基类继承)
例子:
原始对象有:FirstName ,LastName,Birthday,Address properties
Customer.hbm.xml
有:
<property name="FirstName" column="FirstName" type="string" not-null="true" length="64" />
<property name="LastName" column="LastName" type="string" not-null="true" length="64" />
<property name="Birthday" column="Birthday" type="DateTime" not-null="true" />
<property name="Address" column="Address" type="string" not-null="true" />
我的拦截器类有方法:
public bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
在那一刻,甚至可能在保存之前,我必须在 DB Customer 表中添加额外的 2 列(例如计算机名和用户名),propertyNames[]
并且state[]
参数从一开始就没有它们,所以应该即时完成。
MY DB Customer 表包含我上面描述的所有列。
谢谢你。