可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 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
Database.SetInitializer(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".
回答1:
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; } } }
回答2:
Make sure public members of student class are defined as properties w/ {get; set;}
(yours are public variables - a common mistake).
Place a [Key]
annotation on top of your chosen property.
回答3:
There are several reasons this can happen. Some of these I found here, others I discovered on my own.
- If the property is named something other than
Id
, you need to add the [Key]
attribute to it. - The key needs to be a property, not a field.
- The key needs to be
public
- The key needs to be a CLS-compliant type, meaning unsigned types like
uint
, ulong
etc. are not allowed. - This error can also be caused by configuration mistakes.
回答4:
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 = 1)] public int RoleIdName { get; set; }
etc...
回答5:
In my case, I was getting the error when creating an "MVC 5 Controller with view, using Entity Framework".
I just needed to Build the project after creating the Model class and didn't need to use the [Key] annotation.
回答6:
Using the [key] didn't work for me but using an id property does it. I just add this property in my class.
public int id {get; set}
回答7:
The reason why EF framework prompt 'no key' is that EF framework needs a primary key in database. To declaratively tell EF which property the key is, you add a [Key]
annotation. Or, the quickest way, add an ID
property. Then, the EF framework will take ID
as the primary key by default.
回答8:
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.