views:

26

answers:

2

I have two tables in a legacy database...

tblParentTable (int id, string specialIdentifier, ...)
tblChildTable (int id, string specialIdentifier, ...)

As you can see, an auto-incrementing int id has been added, but the child table is joined using the old string primary key (in fact, specialIdentifier is the primary key of tblParentTable, and the primary and foreign key on tblChildTable).

So I have created domain objects and Fluent NHibernate maps, but because of this odd schema, NHibernate thinks that it needs to save the tblChildTable record first, before it saves tblParentTable - this results in a foreign key constraint error.

How can I hint to NHibernate that tblParentTable is the parent and needs to be saved first?

Here is are the mappings...

public ParentMap()
{
    Table("tblParentTable");
    Id(x => x.Id).Column("id");
    Map(x => x.SpecialIdentifier);
    References(x => x.Child).Column("specialIdentifier");
}

public ChildMap()
{
    Table("tblChildTable");
    Id(x => x.Id).Column("id");
    Map(x => x.SpecialIdentifier);
    References(x => x.Child).Column("specialIdentifier");
}

Please feel free to ask for more information if you think I am missing something important.

Thanks

A: 

I don't use fluent; instead I create my mappings by creating the xml mapping files manually. I believe that the 'inverse' property on the association allows you to specify the 'owner' (parent) of the association. Since I don't use fluent, I don't know how to specify that in your mapping, so I cannot give you an example.

Also, in Xml mapping, I'd use the 'property-ref' attribute to indicate that the 'specialIdentifier' is the column that is used by the relationship.

Frederik Gheysels
A: 

If you are looking to do this kind of mapping in Fluent NHibernate, there is a good article on it here:

http://wiki.fluentnhibernate.org/Fluent_mapping

Sohnee