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

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

问题描述

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

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).

我将如何在 NHibernate 中处理这个问题?是否有捷径可寻?如果没有,有没有人知道我可以这样做的方法(我已经考虑过使用拦截器,但真的不知道从哪里开始......).我无法更改数据库架构,因此很遗憾,这不是一个选项(流畅的版本也可以......).

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...).

推荐答案

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

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.

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

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;

您正在尝试插入域中不存在但数据库模型需要的数据,因此上述方法对您不太适用.相反,您必须向每个 state、propertyNames 和 types 数组添加一个元素.

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天全站免登陆