将未映射的数据添加到 NHibernate
我有一个对象正在尝试使用 NHibernate 持久保存到旧数据库中。我拥有域对象中映射的表中的所有相关列,但表中有一些必须填充旧数据库结构的字段。
根据一些建议,我创建并注册了一个 NHibernate 拦截器来为我执行此操作。
public class CustomLineItemInterceptor : EmptyInterceptor
{
public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
var lineItem = entity as SomeCustomLineItem;
if (lineItem == null)
return false;
List<string> propertyNameList = propertyNames.ToList();
List<IType> typeList = types.ToList();
List<object> stateList = state.ToList();
propertyNameList.Add("Source"); // the unmapped column in the database
typeList.Add(NHibernateUtil.String);
stateList.Add("My Application's Name"); // the value I need to persist
state = stateList.ToArray();
propertyNames = propertyNameList.ToArray();
types = typeList.ToArray();
return true;
}
}
我看过这个NHibernate在拦截器中添加未映射的列和NHibernate 中未映射的列?没有找到答案。
我看到的是,OnSave 方法确实触发了,但我没有在数据库中存储任何内容(或由 NHibernate 生成的 SQL 查询)。
我做错了什么?
I have an object I'm trying to persist to a legacy database with NHibernate. I have all of the relevant columns from a table mapped in my domain object, but have a few fields in the table that must be populated for the structure of the legacy db.
Following a few recommendations, I created and registered an NHibernate interceptor to do this for me.
public class CustomLineItemInterceptor : EmptyInterceptor
{
public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
var lineItem = entity as SomeCustomLineItem;
if (lineItem == null)
return false;
List<string> propertyNameList = propertyNames.ToList();
List<IType> typeList = types.ToList();
List<object> stateList = state.ToList();
propertyNameList.Add("Source"); // the unmapped column in the database
typeList.Add(NHibernateUtil.String);
stateList.Add("My Application's Name"); // the value I need to persist
state = stateList.ToArray();
propertyNames = propertyNameList.ToArray();
types = typeList.ToArray();
return true;
}
}
I've looked at this NHibernate add unmapped column in interceptor and Unmapped Columns in NHibernate? without finding the answer.
What I'm seeing is that I the OnSave method does fire, but I get nothing stored in the database (or the SQL query generated by NHibernate).
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还有使用虚拟 Properties 和 IPropertyAccessor 的这种方法。我在域模型的一个项目中使用了它,对我来说,在映射中声明一次比在打开会话的任何地方都更容易,这发生在几个不同的项目中(应用程序、导入、ETL、其他程序的集成)
there is also this approach using virtual Properties and IPropertyAccessor. I used it in one of my projects in the domain model and for me its easier to declare it once in the mapping than everywhere where i open a session, which happens in several different projects (Application, imports, ETLs, integration of other programs)
我不太了解 NHibernate 拦截器的工作原理,但我不认为更改传入的数组变量实际上会更改该方法外部的任何内容。为了使新数组最终返回到方法之外,需要通过引用 (
ref
) 参数传递它们。查看您的第一个链接问题(在拦截器中添加未映射的列),第一个跳出来的区别是,在他的方法的末尾,他调用了传入更新参数的基本方法(
return base.OnSave(entity, id, state, propertyNames,类型);
)。也许这会有帮助?I don't know a lot about how NHibernate Interceptors work, but I wouldn't expect that changing the passed in array variables would actually change anything external to the method. In order for the new arrays to end up back outside of the method, they would need to be passed by reference (
ref
) parameters.Looking at your first linked question (add unmapped column in interceptor), the first difference that jumps out is that at the end of his method, he calls the base method passing in the updated parameters (
return base.OnSave(entity, id, state, propertyNames, types);
). Perhaps that would help?