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

I had a strange problem when I update my EF model (version 5). It removes all classes belonging to this model.

My situation was like this. I changed the key column from two tables containing links to my main table. Updating the model did not make these changes in edmx, so I deleted these three tables (primary and two lookup tables), saved edmx. Then I updated the model and added these tables again.

As soon as I saved the model after updating it, VS deleted all the class files for this edmx. Edmx still looks good with all the tables, only the classes are gone. Then I need to revert my latest changes and try again.

One thing that worked was to manually edit the edmx file to make the appropriate changes to the affected classes, but I don't understand why I need it.

Any ideas there?

Thanks!

+6
source share
6 answers

One way this happens after deleting and then adding the table again to get EDMX to get the change in the foreign key (FK) column.

This is because relationships with other tables are not updated.

For this answer, fooobar.com/questions/951323 / ... , to fix it manually, filter the list of errors using "Build Only" and then manually resolve any build errors in the EDMX editor. Pay attention to the multiplicity of relationships.

Alternatively, use the version control to revert all EDMX changes and start over using the following approach:

Instead of deleting and re-adding only the table containing the modified FK column, delete and re-add all the tables that have something to do with any FK column that has changed.

+6
source

You can try one of the following:

1) Right-click on the .edmx file in the solution explorer in Visual Studio and select the "Run Custom Tool" menu item.

2) Under the title bar of the Solution Explorer, you can see the button with the prompt "Transform All Templates". Click on it. It should update your models in the file 'YourEdmxFileName.tt'.

3) From the Visual Studio "Build" menu, select "Transform All T4 Templates".

4) Locate the individual T4 template files (* .tt), right-click them, and select Run Custom Tool.

0
source

The same thing happened to me. I had a table with a string value, but I wanted to change it to a foreign key so that I could store duplicate string values ​​in a separate table. So I created a new table, and I changed the column type of the row to an invalid int and added the foreign key. When I updated the model, it marked all entity classes as deleted ... not funny.

I also saw some errors appearing indicating that EF did not change the column type from row to int.

The solutions mentioned above (custom tool and conversion of all templates) did not help me.

For me, the solution was to first change the column type from row to int manually in the model, and then update the model, there were no more problems.

For the record: this happened in EF 4 in Visual Studio 2010.

0
source

To resolve this situation, I moved all the table definition files from the directory where my model was located. Then I told the Team browser to delete it. He added the files back to my directory and I was able to continue.

0
source

Make sure you do not have a variable with the _ naming convention. If there is any, EF will not work and will overwrite all classes again.

Changing the variable name p_ helped me.

0
source

I had a similar problem.

Im using VS2013, EF6 and connecting to ORACLE 11G (database first).

I added a table with a foreign key to another table. For some reason, the type of foreign keys was different in these two tables.

Updating a foreign key type solves my problem.

0
source

Source: https://habr.com/ru/post/951321/


All Articles