Why need key in Entity in Entity Framework RRS feed

  • Question

  • Hi There,

    We are creating some entities in Entity Framework. It is not associate to any Database means we create blank edmx file and adding new Entity in the same. We create new entity with name AuditData which contains following fields.

     Scalar Property : Type

     FieldName       : string

    CreateDate      : DateTime

    CreatedBy       : Int

    UpdatedDate     : DateTime

    UpdatedBy       : Int


    But when I am trying to save this entity, it Run custom tool automatically and give following error.

     Error 1 Running transformation: EntityType 'EntityModel.AuditData' has no key defined. Define the key for this EntityType. .\..\Model\EntityModel.edmx

    After that I add new property by name ID and mark it as EntityKey. After that it generates class successfully, which I don't require in this entity. Can please how I can avoid this error without adding any key in entity.



    Thursday, September 18, 2014 7:28 AM


  • Hello Ankush,

    >>Can please how I can avoid this error without adding any key in entity.

    Considering that you are working with model first, then this is not possible to create an entity without declaring a primary key for it. Because Entity Framework would work with the entity through its key. An entity without primary key only could exists in the model by being imported from a database view. So if you want to create a view entity, you need to use database fisrt approach.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 19, 2014 2:17 AM