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

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.

Leave a Comment