EntityType没有键定义错误

时间:2013-11-25 21:16:23

标签: c# .net 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);
        }

    }
}

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; }
    }
}

型号:

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;
    }
}

数据库表:

CREATE TABLE [dbo].[studentdetails](
    [RollNo] [int] NULL,
    [Name] [nvarchar](50) NULL,
    [Stream] [nvarchar](50) NULL,
    [Div] [nvarchar](50) NULL
)  

在global.asax.cs

Database.SetInitializer<MvcApplication1.Models.studentContext>(null);

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

  

“在模型生成期间检测到一个或多个验证错误”以及“实体类型未定义任何键”。

14 个答案:

答案 0 :(得分:151)

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; }
    }
}

答案 1 :(得分:89)

  1. 确保学生班级的公共成员被定义为属性 w / {get; set;}(您的公共变量 - 一个常见的错误)。< / p>

  2. 在您选择的媒体资源上方放置[Key]注释。

答案 2 :(得分:77)

这可能有几个原因。其中一些我发现here,其他我自己发现的。

  • 如果该属性的名称不是Id,则需要向其添加[Key]属性。
  • 密钥需要是属性,而不是字段。
  • 密钥必须为public
  • 密钥必须是符合CLS的类型,这意味着不允许使用uintulong等无符号类型。
  • 此错误也可能由configuration mistakes引起。

答案 3 :(得分:19)

我知道这篇文章迟到了 这个解决方案帮助了我:

    [Key]
    [Column(Order = 0)]
    public int RoleId { get; set; }

添加[Column(Order = 0)] 之后[关键] 可以通过增加1:

来添加
    [Key]
    [Column(Order = 1)]
    public int RoleIdName { get; set; }

等...

答案 4 :(得分:15)

在我的情况下,使用Entity Framework&#34;创建带视图的&#34; MVC 5控制器时出现错误。

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

答案 5 :(得分:8)

使用[key]对我来说没有用,但使用id属性就可以了。 我只是在课堂上添加这个属性。

public int id {get; set;}

答案 6 :(得分:2)

EF框架提示&#39;没有密钥&#39;是EF框架需要数据库中的主键。要以声明方式告知EF键的属性,请添加[Key]注释。或者,以最快的方式添加ID属性。然后,EF框架默认将ID作为主键。

答案 7 :(得分:2)

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

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

public class myClass
{
    public int Id { get; set; }
    [Key]
    public string Name { get; set; }
}

https://stackoverflow.com/a/51180941/7003760

答案 8 :(得分:1)

您不必一直使用key属性。确保映射文件正确寻址了键

this.HasKey(t => t.Key);
this.ToTable("PacketHistory");
this.Property(p => p.Key)
            .HasColumnName("PacketHistorySK");

,并且不要忘记在存储库的OnModelCreating中添加映射

modelBuilder.Configurations.Add(new PacketHistoryMap());

答案 9 :(得分:1)

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

 guard let url = URL(string: "http://i.imgur.com/w5rkSIj.jpg") else {
        return
    }

    let data = try? Data(contentsOf: url)

    if let imageData = data {
        let image = UIImage(data: imageData)
    }

您必须这样使用Public关键字

[Key] 
int RoleId { get; set; } //wrong method

答案 10 :(得分:0)

一切都是对的,但就我而言,我有两个这样的课程

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; }
}
        }
    }

删除上层阶级后,它对我来说很好。

答案 11 :(得分:0)

对我来说,模特很好。这是因为我有2个不同版本的实体框架。 Web项目的一个版本,另一个包含模型的公共项目。

我只需要合并nuget包

enter image description here

享受!

答案 12 :(得分:0)

我遇到了类似的错误。后来我发现这是因为我将模型中的属性声明为私有。将属性从私有更改为公共可以解决该错误。

答案 13 :(得分:0)

我也通过解决代码中的这一行来解决自己的项目中的问题。 我添加了以下内容。

[DatabaseGenerated(DatabaseGeneratedOption.None)]

意识到我的错误后,我将其更改为

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

这进一步确保了每次在数据库中插入新行时,名为“ Id”的字段的值都会增加

相关问题