asp.net-mvc EntityType 'Category' 没有定义键。定义此 EntityType 的键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13862013/
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:28:26  来源:igfitidea点击:

EntityType 'Category' has no key defined. Define the key for this EntityType

asp.net-mvcentity-framework

提问by Saideep Kankarla

Developing a basic ASP.net MVC 4 application. Its a simple product catalog application, where in I have 2 database tables ('Category' and 'Products')

开发一个基本的 ASP.net MVC 4 应用程序。它是一个简单的产品目录应用程序,其中我有 2 个数据库表(“类别”和“产品”)

There is a foreign key reference of 'Category id' (primary key in Category table) in 'Products' table.

“产品”表中有一个“类别 ID”(类别表中的主键)的外键引用。

When I run the application, I am getting error message (listed below).

当我运行应用程序时,我收到错误消息(如下所列)。

System.Data.Entity.Edm.EdmEntityType: : EntityType 'Category' has no key defined. Define the key for this EntityType.

System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Category' is based on type 'Category' that has no keys defined

This looks like a common error for newbies,I did check all the related solutions to 'Entity key '' has no key defined.' but still my problem is not solved, Kindly help me in understanding this problem and what is the correct solution to this problem.

这对于新手来说似乎是一个常见错误,我确实检查了“实体键”的所有相关解决方案没有定义键。但我的问题仍然没有解决,请帮助我理解这个问题以及这个问题的正确解决方案是什么。

Below are my model classes

下面是我的模型类

Category.cs

分类.cs

namespace ChemicalStore.Models
{
    public partial class Category
    {
        public int CatId { get; set; }
        public string CatName { get; set; }
        public string CatDescription { get; set; }
        public List<Product> Product { get; set; }
    }     
}

Products.cs

产品.cs

namespace ChemicalStore.Models
{
    public class Product
    {
        public int ProductId { get; set; }
        public int CatId { get; set; }
        public string ProductTitle { get; set; }
        public string ProductPrice { get; set; }
        public string ProductDescription { get; set; }
        public string ProductPackage { get; set; }
    }
}

回答by Kirill Bestemyanov

You should add attribute [Key] before property CatId:

您应该在属性 CatId 之前添加属性 [Key]:

        using System.ComponentModel.DataAnnotations;

        public partial class Category
        {
            [Key]
            public int CatId { get; set; }
            public string CatName { get; set; }
            public string CatDescription { get; set; }
            public List<Product> Product { get; set; }
        }

The problem is that EF can work only when it knows primary key of table. By default EF recognize as primary key property with name Id. If your table has another primary key, you can mark it with attribute [Key] or set Key with fluent configuration.

问题是EF只有在知道表的主键时才能工作。默认情况下,EF 识别为具有名称 Id 的主键属性。如果你的表有另一个主键,你可以用属性[Key]标记它或者用流畅的配置设置Key。

回答by Ricardo Souza

Entity Framework uses a primary key field to create the primary key column on the generated table.

实体框架使用主键字段在生成的表上创建主键列。

It uses convention to get this column with some variations:

它使用约定来获得具有一些变化的此列:

  • If the field is called idor any casing variance;
  • If the field is called ClassNameIdof any casing variance;
  • 如果该字段被调用id或任何大小写差异;
  • 如果该字段被称为ClassNameId任何大小写差异;

I may be missing some more conventions, but these are the most common cases.

我可能会遗漏一些更多的约定,但这些是最常见的情况。

If you don't have any field with this conventions you need to mark your desired primary key with the attribute [Key]:

如果您没有任何具有此约定的字段,则需要使用属性标记所需的主键[Key]

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

回答by Corey Ford

I was able to solve this by adding a setter to my key property; I only had a getter before.

我能够通过在我的 key 属性中添加一个 setter 来解决这个问题;我以前只有一个吸气剂。

public int Id { get; set; }

回答by Mahesh.P

Just place [key] attribute on above id ..

只需将 [key] 属性放在上面的 id ..

[Table("employee")]

public class Employee
{ 

    [Key]

    public int Empno { set; get; }

    public string Empname { set; get; }      
}

回答by Guish

Entity classes are auto generated..

实体类是自动生成的..

Manual changes to those files will be overwritten if the code is regenerated.

如果重新生成代码,对这些文件的手动更改将被覆盖。

You need to create a partial class with all your metadata

您需要使用所有元数据创建一个部分类

    [MetadataType(typeof(Category.CategoryMetadata))]
    public partial class Category
    {
        internal sealed class CategoryMetadata
        {
            [Key]
            public int CatId { get; set; }
            [Required]
            public string CatName { get; set; }
            public string CatDescription { get; set; }
            public List<Product> Product { get; set; }
        }
    }

Read more on MSDN

MSDN上阅读更多内容

回答by KULDEEP

Hi if you are getting below error. ""One or more validation errors were detected during model generation:

嗨,如果您遇到以下错误。""在模型生成过程中检测到一个或多个验证错误:

Checking.Models.Employee: : EntityType 'Employee' has no key defined. Define the key for this EntityType."" just check your table column name and defined property name same or not . if not then correct it proble resolved.

Checking.Models.Employee:: EntityType 'Employee' 没有定义键。定义此 EntityType 的键。"" 只需检查您的表列名称和定义的属性名称相同与否。如果没有然后纠正它问题解决。

回答by nano

In my case I fixed this problem adding the right connectionString in the web.config/app.config file.

就我而言,我解决了这个问题,在 web.config/app.config 文件中添加了正确的 connectionString。

I had forgotten adding it and the DBContext was not able to communicate with the DB.

我忘记添加它,并且 DBContext 无法与数据库通信。

Hope it helps

希望能帮助到你