August 20, 2014

I am working on an older product and updating it to use the latest version of .NET and Entity Framework. Now that I have created the Entity Framework model using database first approach, I am finding issues like "EntityType 'EntityName' has no key defined". Basically, what this means is that the table in the database does not have a primary key defined. To fix the issue, the database needs to be updated and a primary key needs to be defined.

In my scenario, this might be a bit tricky as I am modifying an existing application but it is better to fix it now and then leave it later as it needs to be done anyway. Either an unique field in the table can be turned into a primary key or an auto-generated field can be used instead. Once the database is updated and entity model updated, the issue mentioned should be fixed.

In case, you are using code first approach to create tables, then the attribute Key must be applied to the particular field as shown below.

public int ProductId { get; set; }
public string ProductName { get; set; }


Reference: Shahed Kazi at