NHibernate, legacy database, foreign keys that aren't

Posted by Joe on Stack Overflow See other posts from Stack Overflow or by Joe
Published on 2010-04-09T17:46:59Z Indexed on 2010/04/16 22:23 UTC
Read the original article Hit count: 318

Filed under:

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?

© Stack Overflow or respective owner

Related posts about fluent-nhibernate