[英]EntityType has no key defined error
Controller:控制器:
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);
}
}
}
DbContext Model:数据库上下文模型:
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; }
}
}
Model:型号:
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;
}
}
Database table:数据库表:
CREATE TABLE [dbo].[studentdetails](
[RollNo] [int] NULL,
[Name] [nvarchar](50) NULL,
[Stream] [nvarchar](50) NULL,
[Div] [nvarchar](50) NULL
)
In global.asax.cs在 global.asax.cs
Database.SetInitializer<MvcApplication1.Models.studentContext>(null);
The above code lists all the classes I am working on.上面的代码列出了我正在处理的所有类。 Upon running my application am receiving the error:
运行我的应用程序时收到错误:
"One or more validation errors were detected during model generation" along with "Entity type has no key defined".
“在模型生成期间检测到一个或多个验证错误”以及“实体类型未定义键”。
The Model class should be changed to :模型类应更改为:
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; }
}
}
Make sure public members of student class are defined as properties w/ {get; set;}
使学生类的肯定公共成员被定义为性能W /
{get; set;}
{get; set;}
(yours are public variables - a common mistake). {get; set;}
(你的是公共变量- 一个常见的错误)。
Place a [Key]
annotation on top of your chosen property.在您选择的属性顶部放置一个
[Key]
注释。
There are several reasons this can happen.发生这种情况的原因有多种。 Some of these I found here , others I discovered on my own.
有些是我在这里找到的,有些是我自己发现的。
Id
, you need to add the [Key]
attribute to it.Id
,则需要向其添加[Key]
属性。public
public
uint
, ulong
etc. are not allowed.uint
、 ulong
等无符号类型。i know that this post is late but this solution helped me:我知道这篇文章来晚了,但这个解决方案帮助了我:
[Key]
[Column(Order = 0)]
public int RoleId { get; set; }
added [Column(Order = 0)] after [Key] can be added by increment by 1:在 [Key] 之后添加 [Column(Order = 0)] 可以增加 1:
[Key]
[Column(Order = 1)]
public int RoleIdName { get; set; }
etc...等等...
In my case, I was getting the error when creating an "MVC 5 Controller with view, using Entity Framework".就我而言,我在使用实体框架创建“带有视图的 MVC 5 控制器”时遇到错误。
I just needed to Build the project after creating the Model class and didn't need to use the [Key] annotation.我只需要在创建模型类后构建项目,不需要使用 [Key] 注释。
Using the [key] didn't work for me but using an id property does it.使用 [key] 对我不起作用,但使用 id 属性可以。 I just add this property in my class.
我只是在我的班级中添加了这个属性。
public int id {get; set;}
The object must contain a field which will be used as the Primary Key
, if you have a field named Id
then this will by default be the primary key of the object to which Entity Framework will link to.该对象必须包含一个将用作
Primary Key
的字段,如果您有一个名为Id
的字段,则默认情况下这将是实体框架将链接到的对象的主键。
Otherwise, you should add the [Key]
attribute above that field you wanna use as the Primary Key
, and you'll also need to add the namespace System.ComponentModel.DataAnnotations
:否则,您应该在要用作
Primary Key
字段上方添加[Key]
属性,并且您还需要添加命名空间System.ComponentModel.DataAnnotations
:
public class myClass
{
public int Id { get; set; }
[Key]
public string Name { get; set; }
}
https://stackoverflow.com/a/51180941/7003760 https://stackoverflow.com/a/51180941/7003760
Additionally Remember, Don't forget to add public keyword like this另外记住,不要忘记像这样添加public关键字
[Key]
int RoleId { get; set; } //wrong method
you must use Public keyword like this你必须像这样使用 Public 关键字
[Key]
public int RoleId { get; set; } //correct method
The reason why EF framework prompt 'no key' is that EF framework needs a primary key in database. EF框架提示'no key'的原因是EF框架需要数据库中的主键。 To declaratively tell EF which property the key is, you add a
[Key]
annotation.要以声明方式告诉 EF 键是哪个属性,请添加
[Key]
注释。 Or, the quickest way, add an ID
property.或者,最快的方法是添加
ID
属性。 Then, the EF framework will take ID
as the primary key by default.然后,EF 框架会默认以
ID
作为主键。
For me, the model was fine.对我来说,模型很好。 It was because I had 2 different versions of entity framework.
这是因为我有 2 个不同版本的实体框架。 One version for the web project and another one for the common project which contains the models.
一个版本用于 Web 项目,另一个版本用于包含模型的公共项目。
I just had to consolidate the nuget packages .我只需要整合 nuget 包。
Enjoy!享受!
You don't have to use key attribute all the time.您不必一直使用 key 属性。 Make sure the mapping file properly addressed the key
确保映射文件正确地解决了密钥
this.HasKey(t => t.Key);
this.ToTable("PacketHistory");
this.Property(p => p.Key)
.HasColumnName("PacketHistorySK");
and don't forget to add the mapping in the Repository's OnModelCreating并且不要忘记在 Repository 的 OnModelCreating 添加映射
modelBuilder.Configurations.Add(new PacketHistoryMap());
All is right but in my case i have two class like this一切都很好,但就我而言,我有两个这样的课程
namespace WebAPI.Model
{
public class ProductsModel
{
[Table("products")]
public class Products
{
[Key]
public int slno { get; set; }
public int productId { get; set; }
public string ProductName { get; set; }
public int Price { get; set; }
}
}
}
After deleting the upper class it works fine for me.删除上层后,它对我来说很好用。
I too solved this issue in my own project by solving this particular line in my code.我也在自己的项目中通过解决代码中的这一行来解决这个问题。 I added the following.
我添加了以下内容。
[DatabaseGenerated(DatabaseGeneratedOption.None)]
After realizing my mistake I then went and changed it to在意识到我的错误后,我去把它改成了
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
This further ensures that the field named "Id" increments in value each time a new row is inserted in the database这进一步确保了每次在数据库中插入新行时名为“Id”的字段的值都会增加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.