实体自定义导航属性

时间:2015-02-23 08:34:32

标签: entity-framework-6

我正在尝试研究如何在此场景中向主体实体添加导航属性 - (可能使用双主键无法实现):

public class EntityA
{
    [Key]
    public int EntityAPrimaryKey1 { get; set; }

    [Key]
    public int EntityAPrimaryKey2 { get; set; }

    //What goes here?
    public virtual EntityB EntityB { get; set;}
}


public class EntityB
{
    [Key]
    public int EntityBPrimaryKey { get; set; }

    [Column(Order = 1)]
    public int EntityAPrimaryKey1 { get; set; }

    [Column(Order = 2)]
    public int EntityBPrimaryKey2 { get; set; }

    [ForeignKey("EntityAPrimaryKey1,EntityAPrimaryKey2")]
    public virtual EntityA EntityA { get; set; }
}

1 个答案:

答案 0 :(得分:0)

由于EF无法通过名称判断,您可以添加此注释。

[ForeignKey("EntityBPrimaryKey ")]
public virtual EntityB EntityB { get; set;}

您也可以使用流畅的配置进行设置。