Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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; }
    }
}
like image 327
Saideep Kankarla Avatar asked Dec 13 '12 14:12

Saideep Kankarla


3 Answers

You should add attribute [Key] before property CatId:

        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.

like image 160
Kirill Bestemyanov Avatar answered Nov 12 '22 13:11

Kirill Bestemyanov


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 id or any casing variance;
  • If the field is called ClassNameId of any casing variance;

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]
public int CatId { get; set; }
like image 34
Ricardo Souza Avatar answered Nov 12 '22 12:11

Ricardo Souza


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

public int Id { get; set; }
like image 8
Corey Ford Avatar answered Nov 12 '22 11:11

Corey Ford