NHibernate,遗留数据库,外键不是 [英] NHibernate, legacy database, foreign keys that aren't

查看:118
本文介绍了NHibernate,遗留数据库,外键不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的这个项目有一个遗留的数据库,里面有大量的信息用来改变应用程序的行为。基本上我被困在一些我必须小心谨慎的改变。



到我的问题上。在这个数据库是一个表,在这个表是一列。这个列包含整数,大部分预先存在的数据对于这一列的值都是零。



问题是这个列实际上是一个外键引用另一个实体,它在数据库模式中从未被定义为这样。



现在在我的新代码中,我定义了我的Fluent-NHibernate映射,我不必直接在我的代码中处理实体ID。这工作正常,直到我遇到一个实体,在这个列中有一个值为0.


$ b NHibernate认为值为0是一个有效的引用。当我的代码尝试使用被引用的对象时,我得到了一个ObjectNotFoundException,因为显然我的数据库中没有对象,其ID为0.



如何通过映射或某种约定(我使用Fluent-nhibernate),让NHibernate的ID对待0是相同的,如果它是NULL?

解决方案

我发现API告诉NHibernate忽略找不到的引用(NotFound.Ignore()),而不是抛出异常。我很困惑所有提到的SetAttribute()我在网上找到这是一个流利的nhibernate比我使用的老版本。


The project I'm working on has a legacy database with lots of information in it that's used to alter application behavior. Basically I'm stuck with something that I have to be super careful about changing.

Onto my problem. In this database is a table and in this table is a column. This column contains integers and most of the pre-existing data have a value of zero for this column.

The problem is that this column is in fact a foreign key reference to another entity, it was just never defined as such in the database schema.

Now in my new code I defined my Fluent-NHibernate mapping to treat this column as a Reference so that I don't have to deal with entity id's directly in my code. This works fine until I come across an entity that has a value of 0 in this column.

NHibernate thinks that a value of 0 is a valid reference. When my code tries to use that referenced object I get an ObjectNotFoundException as obviously there is no object in my database with an id of 0.

How can I, either through mapping or some kind of convention (I'm using Fluent-nhibernate), get NHibernate to treat id's that are 0 the same as if it was NULL?

解决方案

I found the API to tell NHibernate to ignore references that aren't found (NotFound.Ignore()), rather than throw the exception. I was confused by all the mentions of SetAttribute() I found online which is for an older version of fluent-nhibernate than I am using.

这篇关于NHibernate,遗留数据库,外键不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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