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/
- Close the model designer in Visual Studio if it is still open and re-open the .edmx file in an XML editor;
- Find the edmx:StorageModels -> Schema -> Entity Container -> EntitySet element that refers to the table in question;
- On the EntitySet element, rename the store:Schema attribute to Schema; Remove the store:Name attribute altogether;
- 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.
Author by
user2613228
Updated on June 05, 2022Comments
-
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,
- Doesnt EF use the identity column
- I am sceptical about adding primary key to the table as it might affect upstream / downstream processes.
- Alternatives / suggestions please?
Thanks,
-
ak1984 over 7 yearsThis 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 about 6 yearsThis 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 over 5 yearsFor 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 about 4 yearsThis work sometimes. I had tor clean, rebuild and close solution after I added the [Key] annotation. Not ideal though.
-
Ahmed Ghazey over 3 yearsyou have to add primary key constraint to your view, entity framework will not import your view if it is haven't a primary key.