c# - EF 中的复合键有效吗?

标签 c# entity-framework testing

我首先开始使用 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/

相关文章:

c# - C# 中的字典词典

c# - 如何在 Godot 的节点内将标签居中?

c# - 使用 LINQ 分页而不排序

c# - 通用工作单元和存储库模式与 AutoMapper 抛出异常 "another entity of the same type already has the same primary key value"

javascript - getByText 用于通过动态生成的字符串将文本拆分为单独的行

c# - DI Singleton 实例与 Transient 实例

c# - UrlHelper 模拟不起作用

entity-framework - Entity Framework Code First 如何按降序创建索引?

javascript - 元素不可点击,因为另一个元素遮挡了它,元素重叠

java - 我想测试一个私有(private)方法——我的设计有问题吗?