Error message: ModelValidationException System.Data.Entity.EdmEntityType: EntityType ‘blah’ has no key defined. Define the key for this EntityType

You may see this error message when adding a controller.

You may see this error message via the debug output, or like this when adding a controller.

There are some simple fixes you can apply when this message occurs.

 // reason 1 - You've forgotten to include a key. All EF models need a key
 public class Item
 {
     public string name { get; set; }
     // just uncomment the line below
     // public int Id {get;set;}
 }
// reason 2 - Class name is not equal to Id member name
public class ItemClass
{
   public int ItemId {get;set;}
   // just change ItemId to Id, or change ItemClass to Item
}
// reason 3 - member does not have getter/setter - it needs to be a property
// If this isn't the Key, you may see a NotSupportedException instead:
//    The specified type member '<name>' is not supported in LINQ to Entities. 
//    Only initializers, entity members, and entity navigation properties are supported.
public class Item
{
   public int ItemId;
   // In the above, just add {get;set;}
}
// reason 4 the Id member is not public
public class Item
{
   // your member must be public
   protected int Id;
}
 // reason 5 - you're confusing Entity Framework
 public class Item
 {
     public int anId { get; set; }
     public int anotherId { get; set; }
 }

 // Just add [Key] to give EF a hint.
 // don't forget-  using System.ComponentModel.DataAnnotations;
 public class Item
 {
     [Key] 
     public int anId { get; set; }
     public int anotherId { get; set; }
 }

As you can see you don’t need the [Key] attribute and many automatic features like cascading delete operations are provided without having to provide attributes and data annotations or the fluent API. See the  conventions for code first development for more information.

Reason 6

A kind reader, Dmitry, below discovered another very odd incarnation of this error and shared it with us.

When creating a new controller with the Visual Studio UI, he accidentally chose his database context class in the edit box where he was supposed to specify his model class.

So EF was complaining that his ProductCtx didn’t have the key! Thanks Dimitry!

 

Reason 7

Another kind reader – Steven Bolens – provided this tidbit for reason 7.

 // reason 7 - you include a [required] property in the entity which is not 
 // natively supported by the Entity Framework
 public class Item
 {
     [Key]
     public int Id { get; set; }
     [Required]
     public Version ItemVersion { get; set; }         
 }

If you omit the Required attribute for the Version (System.Version) the EF simply silently ignores this ItemVersion property when generating the database. But when the property is Required then the EF tries to create a DbSet which is not possible because Version doesn’t have a key. The errormessage I get doesn’t specify that System.Version doesn’t have a key but instead it mentions a Versions class in my model namespace (which doesn’t exist).

Replacing the Version type with a string type (and converting the value to and from string) solves the problem. – Maybe there’s a cunning way around this with attribute decoration or derivation? – Giles

 

Reason 8

An anonymous contributor discovered this problem can also occur if you create the model before you install Entity Framework, even if you have done all the above steps. So make sure you install Entity Framework the first thing you do after you have created your project.

 

Advertisements

17 thoughts on “Error message: ModelValidationException System.Data.Entity.EdmEntityType: EntityType ‘blah’ has no key defined. Define the key for this EntityType

  1. They also don’t like underscores: (at least in VB) _.
    When running “code first from database” it will automatically generate a _Alias from an “Alias” column in your database table. It will do that for all keyword conflicts. Unfortunately, VB EF6 doesn’t like ‘_’ underscores so you will get a ModelValidationException. Simply ctr-F and find all underscores in your newly generated classes. Remember ‘_’ also means “this line continues to the next” so don’t delete them. Only the ones attached.

    Public Property _Alias As String

    ‘ right click on ‘_Alias’ -> rename -> Change to -> Aliases

    Public Property Aliases As String

    Check your onModelCreating()

    modelBuilder.Entity(Of my_code_alias)() _
    .Property(Function(e) e.Aliases) _
    .IsUnicode(False)

    Hope this helps

  2. It seems your list of hist is far not full.
    Because I tried all hints you listed but it is still reporting mentioned error. 😦

    Just for a ref here is my code:

    [Table(“Product”)]
    public class Product
    {
    [Key]
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string InternalName { get; set; }
    public DateTime Created { get; set; }
    }

    • Sorry, that was my mistake.
      I did chose “ProductCtx” class instead of “Product” class in a “Add Controller” dialogBox. Names “ProductCtx” and “Product” looks similar, so it is easy to mess up these names in a MessageBox with small font.
      ProductCtx class is derived from DbContext, so it is actually Data Context, not the model. I simply messed up these 2 class names.

      But it is good idea for you to describe “reason 6” – when developer could mess up class names in “Add Controller” dialogBox.

  3. Another reason for getting this error is if you include a required property in the entity which is not natively supported by the Entity Framework:
    public class Item
    {
    [Key]
    public int Id { get; set; }
    [Required]
    public Version ItemVersion {get; set;}
    }

    If you omit the Required attribute for the Version (System.Version) the EF simply silently ignores this ItemVersion property when generating the database. But when the property is Required then the EF tries to create a DbSet which is not possible because Version doesn’t have a key. The errormessage I get doesn’t specify that System.Version doesn’t have a key but instead it mentions a Versions class in my model namespace (which doesn’t exist).

    Replacing the Version type with a string type (and converting the value to and from string) solves the problem.

  4. This problem can also occur if you create the model before you install Entity Framework, even if you have done all the above steps. So make sure you install Entity Framework the first thing you do after you have created your project.

  5. Another important case is when you have foreign keys in your class which refer to keys stored in tables that are a part of a different context!

  6. This error can also occur if you do EVERYTHING above correctly, and then do not do a BUILD before you invoke the Add Controller dialogbox. Seems simple, but those who are new to the Visual Studio world might not think this way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s