asp.net-mvc 实体类型没有定义键 - 代码优先

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16207967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:46:39  来源:igfitidea点击:

entity type has no key defined - Code first

asp.net-mvcentity-framework-4

提问by DevT

I'm new to MVC as well as the entity framework. I searched lot and find few similar questions (e.g. Entity Type Has No Key Defined) but they don't solve my problem.

我是 MVC 以及实体框架的新手。我搜索了很多,发现很少有类似的问题(例如Entity Type Has No Key Defined),但它们并没有解决我的问题。

namespace MvcAppInvoice.Models
{
    public class Customer
    {
        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string SurName { get; set; }
        public virtual CustomerType Type { get; set; }
    }

    public class CustomerType
    {
        public int TypeId { get; set; }
        public string TypeName { get; set; }
        public virtual ICollection<Customer> customers { get; set; }
    }
}

Add Controller

添加控制器

When I try to add the controller it gives the following error:

当我尝试添加控制器时,它会出现以下错误:

Error

错误

回答by archil

Typically, code first by convention implicitely sets key for entity type if property is named Idor TypeName+Id. In your case, TypeIdis neither of them, so you should explicitely mark it as key, using KeyAttributeor with fluent syntax, using EntityTypeConfiguration.HasKey Method

通常,如果属性名为Id或,则代码优先按照约定隐式设置实体类型的键TypeName+Id。在您的情况下,TypeId它们都不是,因此您应该使用KeyAttribute或使用流畅的语法,使用EntityTypeConfiguration.HasKey 方法明确地将其标记为键

回答by Darfdm

I had the same problem. I restarted the project 3 times before I found the solution that worked for me. I had a public class that I manually created called UserContext. When I ran the wizard to add an ADO class the wizard generated a partial class of UserContext. I deleted my usercontext class and I modified the class that VS generated for me.

我有同样的问题。在找到适合我的解决方案之前,我重新启动了该项目 3 次。我有一个手动创建的名为 UserContext 的公共类。当我运行向导以添加 ADO 类时,向导生成了 UserContext 的部分类。我删除了我的 usercontext 类,并修改了 VS 为我生成的类。

namespace UserLayer.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public class UserContext : DbContext
    {

        public DbSet<UserDetail> UserDetails { get; set; }
    }
}

Now it may have had worked without the modification. Perhaps deleting my version of the class would have work. I do not know. In retropect I encountered your error when I tried to add the controller. I suspect this is a bug in VS2012 because a partial class and a public class of the same name can exist. The intent is to allow the programmer to extend the definition of the partial class.

现在它可能在没有修改的情况下工作。也许删除我的课程版本会有用。我不知道。回想起来,当我尝试添加控制器时遇到了您的错误。我怀疑这是 VS2012 中的一个错误,因为可以存在同名的部分类和公共类。其目的是允许程序员扩展分部类的定义。

回答by Kiran.Bakwad

Add namespace

添加命名空间

using System.ComponentModel.DataAnnotations;

Add key attribute to your CustomerID like this

像这样将关键属性添加到您的 CustomerID

[Key]
public int CustomerID { get; set; }

then build the application and try to add controller. This will work

然后构建应用程序并尝试添加控制器。这将工作

回答by saurav singh

there was an error running the selected code generator unable to retrieve metadata for entity type has no key defined.

运行选定的代码生成器时出错,无法检索实体类型的元数据没有定义键。

  • one more important thing sql table only takes [Id] then entity framework will work if you use other primary key like[MenuId] it wont.
  • you should add validation on Model which is Key and Column order
  • column order = 1 because after [key] added its increment by 1 that's why
  • 更重要的一件事 sql 表只需要 [Id] 然后实体框架将工作,如果您使用其他主键,如 [MenuId] 它不会。
  • 您应该在模型上添加验证,即键和列顺序
  • 列顺序 = 1 因为在 [key] 增加了 1 之后,这就是为什么

Model

模型

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

then build this after create a controller under entity frame work it will work

然后在实体框架下创建控制器后构建它,它将工作

i hope it helps.

我希望它有帮助。