14

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.

jwheron
  • 2,553
  • 2
  • 30
  • 40
RPS
  • 1,401
  • 8
  • 19
  • 32

6 Answers6

24

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

[Key]
public int BlogTypeId { get; set; }
Ishraq Ahmad
  • 257
  • 1
  • 2
14

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.

kingdango
  • 3,979
  • 2
  • 26
  • 43
  • 2
    What happens if there is a db that exists already and you still get the same error? I'm using MVC 3 and SQL Server 2008 R2. – Jason Foglia Feb 10 '12 at 00:46
  • Also the column name is "FormID" also using [Key] – Jason Foglia Feb 10 '12 at 00:52
  • Ah! Must be a property... that was _my_ mistake! Thanks. – BruceHill Jul 29 '12 at 17:36
  • Thanks, changing the name of my PK in my Database project from "Key" to "Id" just worked. No idea why it was required, the SQL designer showed a key next to the field, put the "PRIMARY KEY" text in the SQL, and the entity framework designer even drew a little key next to the field; so everything knew it was a damn key, but the autogenerated files didn't have "[KEY]" in them, and obviously, editing an autogenerated file is just stupid -- but this: this worked. Thanks man. – BrainSlugs83 Nov 03 '12 at 23:00
5

The error tells you all you need to know: "Define the key for this EntityType".

In EF all entities must have primary keys of some type.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • 1
    I have put a PK in the SQL Server, but it seems it did not refresh the VS to see this then? – RPS Mar 11 '11 at 20:16
  • 1
    It may not have, try right clicking the model and select 'Update Model from Database', otherwise just right click the PK field and select the "Entity Key" item. – CodingGorilla Mar 11 '11 at 20:20
  • I have a key for the entity type -- both SQL and the entity framework designers even show a little key icon next to it. – BrainSlugs83 Nov 03 '12 at 23:03
  • @BrainSlugs83 I have the same issue, are you found any solution; I'm using database first, and my program runs well until a couple of hours ago, I just clean solution to and build solution, and this error issued – mesut Oct 16 '14 at 12:39
3

In the spirit of sharing solutions to the same problem...

I had the same problem but it wasn't fixed by the [Key] solution when coding a MVC 4 app with VS2012.

I forgot to include the getter and setters on my model members. Just having them public isn't enough and Visual Studio will give you the same error message.

I have identified 5 scenarios that result in this message - covered in a small blog post about it.

GilesDMiddleton
  • 2,279
  • 22
  • 31
1

Intersting little tid-bit to share. On my model, I was porting my code from one .NET framework to another and missed a property that converted a string xml field to an XDocument. The property should have had a NotMappedAttribute applied, but like I said, I forgot and then I started getting this not-very-specific error:

One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'XDocument' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'XDocuments' is based on type 'XDocument' that has no keys defined.

I chassed my tail for about an hour because the error was happening on one of the other models exposed by my DbContext class. Out of frustration, I hunted through every XDocument property in my assembly and BAMB! Found one that did not have the NotMapped attribute.

Just wanted to put that out there to prevent someone else from pulling their hair out.

Example:

// This NotMappedAttribute was missing and is required to prevent EF
// from treating the XDocument class as an entity that requires
// a KeyAttribute.
[NotMapped] //<== missing until now
public XDocument DataXml {
    get { return XDocument.Parse(this.m_Xml); }
    set {
        this.m_Data = value.ToString();
    }
}
RashadRivera
  • 793
  • 10
  • 17
0

Same scenario as @Gilesey. In my case I had to mark the key attribute public,

public int Id { get; set; }

kannankeril
  • 187
  • 1
  • 6