EntityType'Category'没有定义键。定义此EntityType的键 [英] EntityType 'Category' has no key defined. Define the key for this EntityType

查看:391
本文介绍了EntityType'Category'没有定义键。定义此EntityType的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

  System.Data.Entity.Edm.EdmEntityType::EntityType'Category'没有定义键。定义此EntityType的键。 

System.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet'Category'基于没有定义键的类别'Category'

这看起来像是新手的常见错误,我确实检查了所有相关的解决方案为'实体密钥''没有键定义'但仍然我的问题没有解决,请帮助我理解这个问题,这个问题的正确解决方法是什么。



以下是我的模型类



Category.cs

 命名空间ChemicalStore.Models 
{
public partial class Category
{
public int CatId {get;组; }
public string CatName {get;组; }
public string CatDescription {get;组; }
public List< Product>产品{get;组; }
}
}

Products.cs

 命名空间ChemicalStore.Models 
{
public class Product
{
public int ProductId {get;组; }
public int CatId {get;组; }
public string ProductTitle {get;组; }
public string ProductPrice {get;组; }
public string ProductDescription {get;组; }
public string ProductPackage {get;组;
}
}


解决方案

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

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

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


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

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

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

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

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

解决方案

You should add attribute [Key] before property CatId:

        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.

这篇关于EntityType'Category'没有定义键。定义此EntityType的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆