Entity framework issue "The table/view ' does not have a primary key defined."

11,768

I've found a workaround in Davis blog (link below) to supress this error, I'll paste it here. http://www.ericsdavis.net/index.php/2009/03/14/entity-framework-and-primary-key-inference/

  1. Close the model designer in Visual Studio if it is still open and re-open the .edmx file in an XML editor;
  2. Find the edmx:StorageModels -> Schema -> Entity Container -> EntitySet element that refers to the table in question;
  3. On the EntitySet element, rename the store:Schema attribute to Schema; Remove the store:Name attribute altogether;
  4. Remove the opening and closing DefiningQuery tags and everything in between them;

Save and close the .edmx file.

It worked like a charm for me, hope it helps.

Share:
11,768
user2613228
Author by

user2613228

Updated on June 05, 2022

Comments

  • user2613228
    user2613228 almost 2 years

    I am new to Entity Framework. I am using an existing database and a table doesn't have primary key defined. When I use Entity Framework, I get the error "The table/view ' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.". I looked up a few stack overflow solutions / google, I still have some questions,

    1. Doesnt EF use the identity column
    2. I am sceptical about adding primary key to the table as it might affect upstream / downstream processes.
    3. Alternatives / suggestions please?

    Thanks,

  • ak1984
    ak1984 over 7 years
    This fixed my problem. Moreover when I opened the edmx doc in an XML editor I read this line of auto generated comment right next to the <entitytype> objects. It said (<!--Errors Found During Generation: warning 6002: The table/view SOME_SCHEMA.SOME_TABLE' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.-->. I thought this indicates the kind of issue that we are facing. I think what @BrunoC fix does is it removes the mentioned inferences of the auto generated comment.
  • Gert Arnold
    Gert Arnold about 6 years
    This is certainly not the standard way to solve this issue. Restarting VS doesn't magically add a primary key to a table in the database.
  • ClockEndGooner
    ClockEndGooner over 5 years
    For a project I was working on, the warning generated by CSC.Exe indicating that the Entity class would be treated as a readonly view since a Primary Key was not defined made sense. However, to keep the build output messages clear and understandable, I just added the Suppress Warning ID for 6002 (Warning 602 does not have a primary key defined) by following the steps for modifying the EF Model's parent project build settings in "How to: Suppress compiler warnings" at docs.microsoft.com/en-us/visualstudio/ide/…. I hope this was of help and interest.
  • Jaitsujin
    Jaitsujin about 4 years
    This work sometimes. I had tor clean, rebuild and close solution after I added the [Key] annotation. Not ideal though.
  • Ahmed Ghazey
    Ahmed Ghazey over 3 years
    you have to add primary key constraint to your view, entity framework will not import your view if it is haven't a primary key.