asp.net-mvc - EntityType 没有定义键。 [Key] 出示

标签 asp.net-mvc entity-framework

实体

public class Region
{
    [Key]
    public int ID;
    public string Name; 
    public string Description;
}

模型

public class RegionModel
{   [Key]
    public int ID { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }
}

错误

System.Data.Edm.EdmEntityType: : EntityType 'Region' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Regions� is based on type �Region� that has no keys defined.

最佳答案

您的类字段需要更改为属性,以便 EF 正确使用该类;

public class Region
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

关于asp.net-mvc - EntityType 没有定义键。 [Key] 出示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13901065/

相关文章:

c# - 更改 ASP .NET MVC 模型类中的主键名称

asp.net-mvc - 如何在 asp.net mvc 5 中关闭所有用户 session

C# 在属性更改时运行逻辑

c# - 如何模拟多个级别的 DbSet.Include lambda?

c# - Visual Studio 自动格式化没有正确格式化我的 Razor 代码

javascript - 使用 jquery ajax 调用的 Asp.Net MVC Razor 图

c# - Entity Framework 更改 Id 类型

c# - Entity Framework 6.1 中的事务范围回滚

c# - ExpressMapper/EntityFramework - 没有为此对象定义无参数构造函数

c# - OutputCache - 指定要缓存的内容