NHibernate中未映射的列? [英] Unmapped Columns in NHibernate?

查看:89
本文介绍了NHibernate中未映射的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Oracle中的遗留数据库,并且我的一些表中有一些列设置为NOT-NULL,我不希望在我的域模型中使用,但显然,我需要指定某处一些默认值被保存到数据库中(例如一个组表可能有一个缩进列,总是期望一个char(8)值)。

关于这个在NHibernate中?是否有捷径可寻?如果没有,有没有人知道我可以做到这一点(我想过使用一个Inteceptor,但真的不知道从哪里开始...)。我不能改变数据库模式,所以,不幸的是,这不是一个选项(流利的版本也可以...)。 >

如果你不想污染你的POCO,那么拦截器可能是最好的选择。看一下关于拦截器的各种文章,比如这一个

在你的拦截器上,你需要检查这个类型,以便知道哪些字段需要放入虚拟数据。确保在OnFlushDirty方法中使用currentState [] OnFlushDirty是更新)和状态[] OnSave方法(插入)。例如(OnSave):

$ p $ if(entity is Group)
{
state [Array.IndexOf (propertyNames,缩进)] =虚拟值;
返回true;
}
返回false;

编辑:

插入您的域中不存在但数据库模型所需的数据,因此上述内容对您而言并不适用。相反,您必须为每个state,propertyNames和types数组添加一个元素。


I'm working with a legacy database in Oracle and some of my tables have columns that are set to NOT-NULL that I don't want in my domain model, but, obviously, I need to specify somewhere that at least some default value is saved to the database (eg a Groups table may have a "Indentation" column thaqt always expects a char(8) value).

How would I go about this in NHibernate? Is there an "easy" way to do this? If not, does anyone know of a way I could do this (I've thought about using an Inteceptor, but really wouldn't know where to start...). I can't change the database schema so this, sadly, isn't an option (fluent versions are ok too...).

解决方案

If you don't want to pollute your POCOs, an Interceptor probably is the best option. Take a look at various articles about interceptors, like this one.

On your interceptor, you'd want to check the type so you know which fields to put dummy data in. Make sure you use currentState[] in the OnFlushDirty method (OnFlushDirty is "update") and state[] in the OnSave method ("insert"). For example (OnSave):

if (entity is Group)
{
    state[Array.IndexOf(propertyNames, "Indentation")] = "dummy value";
    return true;
}
return false;

Edit:

You're trying to insert data that doesn't exist in your domain but is required by your database model, so the above wouldn't quite work for you. Instead, you'll have to add an element to each of the state, propertyNames, and types arrays.

这篇关于NHibernate中未映射的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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