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

前端 未结 6 2095
误落风尘
误落风尘 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: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();
        }
    }
    

提交回复
热议问题