![](/img/trans.png)
[英]Many-to-many relationship in Entity Framework Core when one entity has composite primary key
[英]Entity Framework - many to many with primary key and composite foreign key
我有两个多对多关系中的实体(Case和Person),关系表上有一个额外的数据字段(CasePerson with PersonType)。
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控制器进行操作。 EF或Fluent API中是否可以在此CasePerson表上定义两个键?
我能够通过添加
[Index("IX_CasePerson", 1, IsUnique=true)]
到PersonId和
[Index("IX_CasePerson", 2, IsUnique=true)]
到CaseId。 该唯一索引强制列配对是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.