Fluent NHibernate mapping a composite ID from inherited composite IDs

Danno picture Danno · Nov 9, 2009 · Viewed 8.7k times · Source

Let's say I have an existing database with the following 3 tables:

Table1:
(PK)T1ID1
(PK)T1ID2

Table2:
(PK)T2ID1

Table3:
(FK)T1ID1
(FK)T1ID2
(FK)T2ID1 (Where the 3 keys come from the tables above)

My question is: How do I map Table3 with Fluent NHibernate?
What is confusing to me is what to do about the fact that its composite keys come from 2 different tables.

I have the following for the mappings for tables 1 and 2:

public class Table1
{
    public virtual long T1ID1 { get; set; }
    public virtual long T1ID2 { get; set; }
}

public class Table2
{
    public virtual long T2ID1 { get; set; }
}

public class Table1Map
{
    public Table1Map()
    {
        Table("Table1");

        CompositeId()
            .KeyProperty(x => x.T1ID1, "T1ID1")
            .KeyProperty(x => x.T1ID2, "T1ID2");
    }
}

public class Table2Map
{
    public Table2Map()
    {
        Table("Table2");

        Id(x => x.T2ID1, "T2ID1");
    }
}

Answer

cdmdotnet picture cdmdotnet · Mar 2, 2010

try looking at Fluent NHibernate and composite ID with single column name I think what you're after is the ".KeyReference()" property. The link above is using an old version of FNH but all you need to be concerned with , with regards to your issue is that the method names are slightly different. all the rest of that article should help you.

Let me know how you go.