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;}
}

如果我必须只在一个字段上进行映射,我可以在父映射类中完成以下操作:

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

如果连接必须在两个键 field1 和 field2 上,我该怎么做?

4

1 回答 1

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;
于 2012-12-11T12:41:12.493 回答