Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix System.Data.Edm.EdmEntityType has no key

Does anybody know how to fix this error:

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

Using MVC 3 with Entity Framework.

like image 634
RPS Avatar asked Mar 11 '11 20:03

RPS


2 Answers

Just put [Key] on top of your property (which is presenting primary key). Something like this,

[Key]
public int BlogTypeId { get; set; }
like image 95
Ishraq Ahmad Avatar answered Oct 14 '22 14:10

Ishraq Ahmad


MVC3 will automatically recognize an entity's Key if it follows the convention 'Id' or 'EntityNameId'. Additionally, the entity must expose this as a PROPERTY AND it must be PUBLIC. I made the mistake of using protected for my property and got this error.

A good example is:

public int Id { get; set; }

OR

public int EntityNameId { get; set; }

Use the [Key] attribute if you can't follow this convention OR if you want to be very explicit in your code.

like image 43
kingdango Avatar answered Oct 14 '22 13:10

kingdango