NHibernate,遗留数据库,没有的外键(NHibernate, legacy database, foreign keys that aren't)

编程入门 行业动态 更新时间:2024-10-26 09:34:14
NHibernate,遗留数据库,没有的外键(NHibernate, legacy database, foreign keys that aren't)

我正在处理的项目有一个遗留数据库,其中包含大量用于改变应用程序行为的信息。 基本上我被卡住了,我必须对改变非常小心。

在我的问题上。 在这个数据库中是一个表格,在这个表格中是一列。 此列包含整数,并且此列中大多数已有数据的值为零。

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

现在,在我的新代码中,我定义了我的Fluent-NHibernate映射,将此列作为参考,以便我不必直接在我的代码中处理实体标识。 这工作正常,直到我遇到一个实体在这个列中的值为0。

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

我怎么能通过映射或某种约定(我使用Fluent-nhibernate),让NHibernate将0的ID与NULL相同?

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?

最满意答案

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

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.

更多推荐

database,数据库,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-08-01 09:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1356758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   legacy   NHibernate   keys   foreign

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!