Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does visual studio delete my classes when I update entity framework model

I'm having a strange problem when I update my EF model (ver 5). It deletes all of the classes that belong to that model.

My situation was like this. I changed the key column of two tables which have references to my primary table. Updating the model did not make these changes to the edmx, so I removed those three table (primary and two lookup tables), saved the edmx. Then updated the model and added those tables back in.

Once I saved the model after updating it, VS deleted all of the class files for this edmx. The edmx still looks alright with all of the tables, just the classes have gone. I then have to revert my last changes and try again.

One thing that did work was manually editing the edmx file to make the appropriate changes to the affected classes, but I don't see why I should have to do that.

Any ideas out there?

Thanks!

like image 790
Bauer Avatar asked Aug 08 '13 13:08

Bauer


People also ask

How do I update Entity Framework TT files?

First Build your Project and if it was successful, right click on the "model.tt" file and choose run custom tool. It will fix it. Again Build your project and point to "model.context.tt" run custom tool. it will update DbSet lists.

How do you regenerate EDMX?

There is no automatically refresh the EDMX (it will be nice from MS if they implement that at some point) and the best and most accurate way to refresh the EDMX is by deleting all tables in the Diagram and then deleting all complex types etc. in the Model Browser.


2 Answers

One way this happens after removing then re-adding a table to get EDMX to pick up a change to a Foreign Key (FK) column.

It happens because the relationships to other tables are not refreshed.

Per this answer, https://stackoverflow.com/a/34144341, to manually fix this, filter the Error List by "Build Only", and then manually resolve any build errors in the EDMX editor. Pay attention to the multiplicity of the relationships.

Alternately, use version control to revert the whole EDMX change and start again, using the following approach:

Rather than removing and re-adding only the table containing the FK column that changed, remove and re-add all tables that have any relationship to any FK column that changed.

like image 119
Ed the Dev Avatar answered Sep 23 '22 01:09

Ed the Dev


It's been a long while, but in the end it turned out I had some errors in my edmx file which I hadn't seen, and that was causing the poco class files to not be generated.

like image 44
Bauer Avatar answered Sep 21 '22 01:09

Bauer