dreamerkumar
dreamerkumar

Reputation: 1550

Fluent Nhibernate: Map a child property on two keys

public class Parent
{
public virtual field1 { get; set;}
public virtual field2 { get; set;}
public virtual Child { get; set;
}

public class Child
{
public virtual childfield1 { get; set;} //composite primary key
public virtual childfield2 { get; set;} //composite primary key
public string somedescription { get; set;}
}

If I had to map just on one field, I could have done below in the Parent map class:

References(x => x.Child).ForeignKey("field1");

How can I do it if the join has to be on two keys field1 and field2?

Upvotes: 1

Views: 518

Answers (1)

Firo
Firo

Reputation: 30803

public class Parent
{
    public virtual Child Child { get; set; }
}

public class Child
{
    public virtual int Key1 { get; set; } //composite primary key
    public virtual int Key2 { get; set; } //composite primary key
    public virtual string SomeDescription { get; set;}
}

// in ParentMap
References(p => p.Child).Columns.Add("child_key1", "child_key2");

// in Child
CompositeId()
    .KeyProperty(x => x.Key1)
    .KeyProperty(x => x.Key2);

and to access the ChildKey1 (column) without loading the child

var key1 = parent.Child.Key1;

Upvotes: 2

Related Questions