外键是EF Code First中的主键?

时间:2015-01-08 08:09:35

标签: c# sql entity-framework ef-code-first domain-driven-design

我需要在我的域中映射3个域模型之间的关系,其中一个域模型是关系模型的聚合根。

public class Entity1 {
    public int Id { get; set; }
}

public class Entity2 {
    public int Id { get; set; }
}

public class SuperEntity {
    public int Id { get; set; }
    // bounded context for relationship classes
}

关系实体应该如下所示

public class Relationship {
    public int RelationshipId { get; set; }
    public Entity1 Entity1 { get; set; }
    public Entity2 Entity2 { get; set; }
}

在此之后,超级实体应该如下所示:

public class SuperEntity {
    public int Id { get; set; }
    public ICollection<Relationship> Relationships { get; set; }
}

现在,映射这一点的一种可能性是使关系成为一个独特的实体,它与自己的密钥以及关系中的两个实体都是唯一索引。但是,密钥仅用于关键目的&#34;没有任何有意义的价值欲望是这样的关系表:

Table_Relationships
                  [  SuperEntity_Id // Foreign-key to SuperEntity
  PrimaryKey      [  Entity1_Id // Foreign-key to Entity1
                  [  Entity2_Id // Foreign-key to Entity2

表示Table_Relationships的主键是SuperEntity_Id + Entity1_Id + Entity2_Id。

是否可以在EF Code First中映射它?

1 个答案:

答案 0 :(得分:-1)

为什么不使用DataAnnotations.KeyAttribute(http://msdn.microsoft.com/library/system.componentmodel.dataannotations.keyattribute%28v=vs.110%29.aspx)?这是在域模型类中定义复杂主键的明确方法。

相关问题