[英]Foreign key as primary key in EF Code First?
I need to map a relationship between 3 domain models in my domain, where as one of the domain models is the aggregate root of the relationship model. 我需要在我的域中映射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
}
The relationship entity should look like this 关系实体应如下所示
public class Relationship {
public int RelationshipId { get; set; }
public Entity1 Entity1 { get; set; }
public Entity2 Entity2 { get; set; }
}
Following this, the super entity should simply look like this: 之后,超级实体应该看起来像这样:
public class SuperEntity {
public int Id { get; set; }
public ICollection<Relationship> Relationships { get; set; }
}
Now, one possibility to map this is to make the relationship a unique entity with it's own key and both entities inside the relationship unique indexes. 现在,映射此关系的一种可能性是使关系成为具有其自己的键的唯一实体,并且使关系内的两个实体都具有唯一索引。 But then the key only serves "for key purposes" without any meaningful value. 但是,密钥仅用于“用于密钥目的”,没有任何有意义的价值。 Desireable would be a relationship table like this: 理想的是这样的关系表:
Table_Relationships
[ SuperEntity_Id // Foreign-key to SuperEntity
PrimaryKey [ Entity1_Id // Foreign-key to Entity1
[ Entity2_Id // Foreign-key to Entity2
Meaning that the primary key of Table_Relationships would be SuperEntity_Id+Entity1_Id+Entity2_Id. 这意味着Table_Relationships的主键将是SuperEntity_Id + Entity1_Id + Entity2_Id。
Is it possible to map this in EF Code First? 是否可以在EF Code First中对此进行映射?
Why not use DataAnnotations.KeyAttribute ( http://msdn.microsoft.com/library/system.componentmodel.dataannotations.keyattribute%28v=vs.110%29.aspx )? 为什么不使用DataAnnotations.KeyAttribute( http://msdn.microsoft.com/library/system.componentmodel.dataannotations.keyattribute%28v=vs.110%29.aspx )? It's clear way to define complex primary key in domain model classes. 这是在域模型类中定义复杂主键的明确方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.