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

.net c# entity-framework sql-server

Question

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,

1
2
8/6/2015 11:05:31 AM

Popular Answer

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.

10
9/5/2016 4:22:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow