2012-12-07 36 views
1
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;} 
} 

一個子屬性圖,我可以在父地圖類如下進行:功能NHibernate:如果我只映射在一個領域上的兩個鍵

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

我該怎麼辦呢,如果連接必須位於兩個鍵字段1和字段2上?

回答

2
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); 

和訪問ChildKey1(列)沒有加載孩子

var key1 = parent.Child.Key1; 
+0

正是我需要的。謝謝FIRO – dreamerkumar