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

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

问题描述

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

有在'产品'表'类别ID(在分类表的主键)的外键引用。

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

  \\ tSystem.Data.Entity.Edm.EdmEntityType:的EntityType类别没有定义键。定义此的EntityType的关键。
\\ tSystem.Data.Entity.Edm.EdmEntitySet:的EntityType:EntitySet的类别是基于类型的类别一个没有定义键

这看起来像是新手常犯的错误,我没有检查所有相关的解决方案,以实体键''没有定义的关键......

但仍然是我的问题不解决,请帮助我理解这个问题,什么是正确的解决了这个问题。

下面是我的模型类

  ** ** Category.cs    命名空间ChemicalStore.Models
    {        公共部分类目录
        {
             公众诠释CATID {搞定;组; }
            公共字符串CatName {搞定;组; }
            公共字符串CatDescription {搞定;组; }
            公开名单<产品与GT;产品{搞定;组; }
        }    }
** ** Products.cs    命名空间ChemicalStore.Models
    {
        公共类产品
        {
            公众诠释产品编号{搞定;组; }
            公众诠释CATID {搞定;组; }
            公共字符串ProductTitle {搞定;组; }
            公共字符串productSku,则{搞定;组; }
            公共字符串产品描述{搞定;组; }
            公共字符串ProductPackage {搞定;组; }        }
    }


解决方案

您应该物业CATID前加属性[关键]:

 公共部分类目录
        {
            [键]
            公众诠释CATID {搞定;组; }
            公共字符串CatName {搞定;组; }
            公共字符串CatDescription {搞定;组; }
            公开名单<产品与GT;产品{搞定;组; }
        }

的问题是,只有当它知道表的主密钥EF可以工作。默认情况下EF认识到与名ID作为主键属性。如果你的表还有一个主键,你可以用属性[键]或SET键,用流利的配置对它进行标记。

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).

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


\tSystem.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“类别”没有定义键。定义此的EntityType关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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