问题描述
我有两个实体(Case和Person)与关系表中的额外数据字段(CasePerson with PersonType)处于多对多关系。
I have two entities (Case and Person) in a many-to-many relationship with an extra data field(CasePerson with PersonType) on the relationship table.
public class Case {
public int Id;
public List<CasePerson> CasePeople;
}
public class Person {
public int Id;
public List<CasePerson> CasePeople;
}
public class CasePerson {
public int Id;
public int CaseId;
public int PersonId;
public string Type;
public Person Person;
public Case Case;
}
我想在关系表上创建一个复合键,一个人与一个人的配对是独一无二的。我还想在CasePerson表上维护一个单独的主键,以便通过我的Web API控制器进行操作。在这个CasePerson表中有EF或Fluent API可以定义两个键吗?
I'd like to create a composite key on the relational table such that the pairing of a person with a case is unique. I'd also like to maintain a separate primary key on the CasePerson table for ease of manipulation through my Web API controllers. Is there a way in EF or Fluent API to define both keys on this CasePerson table?
推荐答案
我能够通过添加
[Index(IX_CasePerson,1,IsUnique = true)]
到PersonId和
[Index(IX_CasePerson,2,IsUnique = true)]
到CaseId。此唯一索引强制列配对是唯一的。
to the CaseId. This unique index forces the column pairing to be unique.
这篇关于实体框架 - 多对多主键和复合外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!