【问题标题】:Add Key to Entity Framework model to satisfy need keys?向实体框架模型添加键以满足需要键?
【发布时间】:2020-02-12 11:48:36
【问题描述】:

我正在尝试使用 NetCore Entity Framework 构建一个新的 API 控制器。但是,我收到此错误:“无法添加,因为它针对 keless 实体类型 RobotResponses”

在数据库中,RobotResponses 是一个没有主键的表。但它有两个名为 SessionId 和 HumanId 的列,它们都是外键,并且具有如下索引: uq_RobotResponses_sessionId_humanId

现在,在我的代码中,我应该像这样设置两个属性键吗?

[Key]   
public long SessionId { get; set; }
[Key]
public long HumanId { get; set; }

谢谢!

【问题讨论】:

    标签: entity-framework-core asp.net-core-2.0


    【解决方案1】:

    您正在寻找的是一种叫做复合键的东西。您可以像这样在 fluent api 中配置它: https://www.learnentityframeworkcore.com/configuration/fluent-api/haskey-method

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2011-02-14
      • 1970-01-01
      • 2011-05-17
      • 2020-07-24
      相关资源
      最近更新 更多