问题
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!
回答1:
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.
回答2:
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.
回答3:
The same thing happened to me. I had a table with a string value but I wanted to change this to a foreign key so that I could store the reoccurring string values in a separate table. So I created a new table and I changed the string column type to a not nullable int and added a foreign key. When I updated the model, it marked all entity classes as deleted... not funny.
I also saw some errors popping up indicating that EF didn't change the column type from string to int.
The solutions mentioned above (the custom tool and transforming all templates) did not work for me.
For me the solution was to change the column type from string to int manually in the model first and then updating the model, no more issues occured.
For the record: this happened in EF 4 in Visual Studio 2010.
回答4:
In order to resolve this situation, I moved all of the table definition files out from the directory that my model was located in. I then told the Team Explorer to revert the deletions. It added the files back into my directory and I was able to proceed.
回答5:
You could try one of the following:
1) Right click on the '.edmx' file in the Solution Explorer in Visual Studio and then select the menu item 'Run Custom Tool'.
2) Just below the Solution Explorer Title Bar, you could see a button with a tool tip 'Transform All Templates'. Click it. It should update your models under 'YourEdmxFileName.tt' file.
3) Under Visual Studio 'Build' menu, select 'Transform All T4 Templates'.
4) Locate the individual T4 template files (*.tt), right click on them and select 'Run Custom Tool'.
回答6:
Make sure that you do not have any variable with a naming convention of _. If there is any, EF will fail and re-write all the classes again.
Changing the variable name p_ helped me.
回答7:
I had a similar problem.
Im using VS2013, EF6, and connecting to ORACLE 11G (Database first).
I added a table, that had a foreign key to another table. For some reason, the foreign key Type was different in those two tables.
Updating the foreign key type solve my problem.
来源:https://stackoverflow.com/questions/18127633/why-does-visual-studio-delete-my-classes-when-i-update-entity-framework-model