NHibernate中未映射的列? [英] Unmapped Columns in NHibernate?
问题描述
关于这个在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屋!