我首先开始使用 EF 4.1 代码。
我有一个像这样的实体表:
public AMapping()
{
Property(x => x.Counter).IsRequired();
HasKey(x => x.AID);
HasKey(x => x.BID);
HasRequired<A>(x => x.A)
.WithMany(y => y.CList)
.HasForeignKey(f => f.AID);
ToTable("A");
}
表 A 列看起来像这样:
AID(PK, FK, int, not null)
Counter(int, not null)
BID(PK, FK, int, not null)
在编写保存集成测试时,出现以下错误:
Cannot insert the value NULL into column 'BID', table 'Sprint3.dbo.A'; column does not allow nulls. INSERT fails.
但我可以看到我正在传递一个整数值。
EF 4.1 代码优先中的复合主键是否有任何限制?
最佳答案
HasKey(x => new { x.AID, x.BID});
从未使用过它,昨天才在文档中看到它:)
关于c# - EF 中的复合键有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8991153/