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

前端 未结 6 1946
误落风尘
误落风尘 2021-01-08 01:17

Does anybody know how to fix this error:

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

相关标签:
6条回答
  • 2021-01-08 01:28

    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.

    0 讨论(0)
  • 2021-01-08 01:32

    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.

    0 讨论(0)
  • 2021-01-08 01:36

    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.

    0 讨论(0)
  • 2021-01-08 01:39

    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-08 01:46

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

    [Key]
    public int BlogTypeId { get; set; }
    
    0 讨论(0)
  • 2021-01-08 01:46

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

    public int Id { get; set; }

    0 讨论(0)
提交回复
热议问题