EntityType没有键定义错误

Cod*_*der 137 .net c# entity-framework

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Controllers
{
    public class studentsController : Controller
    {
        //
        // GET: /students/

        public ActionResult details()
        {
            int id = 16;
            studentContext std = new studentContext();
           student first = std.details.Single(m => m.RollNo == id);
            return View(first);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

DbContext模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcApplication1.Models
{
    public class studentContext : DbContext
    {
        public DbSet<student> details { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Models
{
    [Table("studentdetails")]
    public class student
    {
        public int RollNo;
        public string Name;
        public string Stream;
        public string Div;
    }
}
Run Code Online (Sandbox Code Playgroud)

数据库表:

CREATE TABLE [dbo].[studentdetails](
    [RollNo] [int] NULL,
    [Name] [nvarchar](50) NULL,
    [Stream] [nvarchar](50) NULL,
    [Div] [nvarchar](50) NULL
)  
Run Code Online (Sandbox Code Playgroud)

在global.asax.cs中

Database.SetInitializer<MvcApplication1.Models.studentContext>(null);
Run Code Online (Sandbox Code Playgroud)

上面的代码列出了我正在处理的所有类.运行我的应用程序时收到错误:

"在模型生成期间检测到一个或多个验证错误"以及"实体类型没有定义键".

Cod*_*der 158

Model类应更改为:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Models
{
    [Table("studentdetails")]
    public class student
    {
        [Key]
        public int RollNo { get; set; }

        public string Name { get; set; }

        public string Stream { get; set; }

        public string Div { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 实体框架不支持无符号整数http://msdn.microsoft.com/en-us/library/vstudio/bb896317(v=vs.100).aspx#UnsignedIntsUnsupported (4认同)

小智 92

  1. 学生类的确保公共成员被定义为性能 W/{get; set;}(你是公共变量 -一个常见的错误).

  2. [Key]在所选房产的顶部放置注释.


Blu*_*eft 79

这可能有几个原因.其中一些我在这里找到,其他一些是我自己发现的.

  • 如果属性的名称不是Id,则需要向其添加[Key]属性.
  • 关键需要是财产,而不是领域.
  • 关键是需要 public
  • 关键需要一个符合CLS的类型,这意味着无符号的类型,如uint,ulong等都是不允许的.
  • 此错误也可能由配置错误引起.

  • 我还发现key属性必须是可读写的.当我有一个getter并且ID属性没有setter时,我得到了OP的错误. (3认同)
  • @JohnFx 是绝对正确的。Resharper 在清理期间从某些属性中删除了“私有集”。结果是“EntityType 没有键定义错误”。所以当心清理工具会删除必要的代码。 (2认同)

dan*_*man 19

我知道这篇文章很晚但是这个解决方案帮助了我:

    [Key]
    [Column(Order = 0)]
    public int RoleId { get; set; }
Run Code Online (Sandbox Code Playgroud)

在[Key]之后添加[Column(Order = 0)]可以按增量加1:

    [Key]
    [Column(Order = 1)]
    public int RoleIdName { get; set; }
Run Code Online (Sandbox Code Playgroud)

等等...


小智 16

在我的情况下,我在使用实体框架创建"带视图的MVC 5控制器"时遇到错误.

我只需要在创建Model类之后构建项目,而不需要使用[Key]注释.

  • 是的 - '接下来,构建项目.Web API脚手架使用反射来查找模型类,因此它需要编译的程序集.摘自[本页](https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-2) (2认同)

Wil*_*llo 9

使用[key]对我来说不起作用,但使用id属性就可以了.我只是在我的课堂上添加这个属性.

public int id {get; set;}
Run Code Online (Sandbox Code Playgroud)


May*_*zer 6

该对象必须包含一个将用作 的字段Primary Key,如果您有一个名为的字段,Id则默认情况下这将是实体框架将链接到的对象的主键。

否则,您应该在[Key]要用作 的字段上方添加属性Primary Key,并且您还需要添加命名空间System.ComponentModel.DataAnnotations

public class myClass
{
    public int Id { get; set; }
    [Key]
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

/sf/answers/3582665901/


Uda*_*sun 5

另外请记住,不要忘记添加这样的公共关键字

[Key] 
int RoleId { get; set; } //wrong method
Run Code Online (Sandbox Code Playgroud)

您必须像这样使用 Public 关键字

[Key] 
public int RoleId { get; set; } //correct method
Run Code Online (Sandbox Code Playgroud)