【问题标题】:InvalidOperationException: The entity type 'x' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'InvalidOperationException:实体类型“x”需要定义主键。如果您打算使用无密钥实体类型调用“HasNoKey()”
【发布时间】:2020-06-16 12:49:17
【问题描述】:

不知道为什么我会得到这个错误,即使我在键列上有“键”属性。类看起来像这样:

public class MyClass
{
    [Key]
    private string MyPrimaryKey { get; set; } 

    ...
}

【问题讨论】:

  • 你必须将MyPrimaryKey定义为public
  • 啊,当然,谢谢

标签: c# entity-framework-core


【解决方案1】:

请将MyPrimaryKey 定义为public

public class MyClass
{
    [Key]
    public string MyPrimaryKey { get; set; } 

    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2018-07-13
    • 2021-11-27
    • 1970-01-01
    • 2022-07-19
    • 2021-07-23
    • 2018-08-13
    相关资源
    最近更新 更多