I'm trying to setup a bidirectional relationship in NHibernate on 2 classes 
both with composite keys that overlap.
Another difficulty is that I can't just edit the SQL tables to for example 
add a proper ID.

This is probably an easy question but i can't find the answer anywhere.

I've got the HasMany down, as far as i can see. But I'm stuck with the 
HasOne. Also tried with Reference.

public class Parent
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public List<Child> Children { get; set; }
    public string Data { get; set; }
}
public class Child
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public string D { get; set; }
    public Parent Parent { get; set; }
    public string Data { get; set; }
}

public class ParentMap : ClassMap<Parent>
{
    public ParentMap()
    {
        CompositeId()
            .KeyProperty(x => x.A)
            .KeyProperty(x => x.B)
            .KeyProperty(x => x.C);
        Map(x => x.Data);
        HasMany(x => x.Children)
                .KeyColumn("A")
                .KeyColumn("B")
                .KeyColumn("C");
    }
}
public class ChildMap : ClassMap<Child>
{
    public ChildMap()
    {
        CompositeId()
            .KeyProperty(x => x.A)
            .KeyProperty(x => x.B)
            .KeyProperty(x => x.C)
            .KeyProperty(x => x.D);
        Map(x => x.Data);
        HasOne(x => x.Parent);
        // also tried
        //References<Parent>(x => x.Parent)
                //.Columns("A", "B", "C");
    }
}

The error i get now is: 
NHibernate.FKUnmatchingColumnsException: 'Foreign key (FK_3346D8AD:Child 
[C])) must have same number of columns as the referenced primary key 
(Parent [A, B, C])'

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/nhusers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nhusers/8e34644a-0dbc-49bc-838b-13fa0fc2dba2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to