Manually deleted entity structures can be generated from EF migration

I created a migration and created a database and tables. For example, tables

ABCDE . Now I again changed a part of the code and ran the update-database command. Everything went smoothly and beautifully, and the tables had columns. Now, by chance, I manually deleted two D and E tables. Now when I try to migrate using update-database . It works correctly, but does not create tables that I deleted manually. I tried to delete the existing migration and re-run update-database . This gives an error other than two tables. There already exists an object existing in the name "A, B, C" bla bla.

Any idea how to get rid of this situation without dropping the database and re-creating deleted tables using migration? Cause. I do not want to delete the database, because it contains data in the rest of the tables. How to act in this situation, when I have existing tables in the database, and by chance I manually deleted several tables from the SQL server from SSMS.

How to create tables again using hyphenation?

Oh my version of entity frame 6.0.2

+6
source share
3 answers

I finally figured out the solution. Basically this is a change in strategy, how we use migration. Migration Add-migration only checks bridges and previous timestamp timestamp files. so until we provide the update-database command in nuget. he will never know which tables were deleted manually. Therefore, in the context, when we try to perform some migration, for example alter in tables. This causes a problematic migration because this table does not exist in the database, but the migration assumes that it already exists. So for this there is manual work. Following are the steps after we manually deleted the table from the database

  • Check the models that exist in the objects corresponding to the database table. If we find out there is some kind of anomaly in the database table that is not in the database, but exists as an entity that means. They are both not in sync with each other. Therefore, we must find the ratio model => Table for each.

  • If we created an initial migration with all tables, copy the remote createTable code from the migration file.

  • Paste it into the last recently generated migration file. Then create a script or run update-database command . This will create a remote database table. However, there is no automatic command that will synchronize between all objects and tables in the database. This is what we need to track partially manually during the migration process.

+1
source

IMHO the easiest solution is to generate a SQL script, generate the migration and run only the part of the script that creates the missing tables.

 Update-Database -Source MigrationBeforeCreatingTables -Target MigrationAfterCreatingTables -Script 
+1
source

EF The migration history is stored in the _MigrationHistory table. Remove the table from the database. Warning: this will erase the entire migration history and you will have to recreate all the tables

+1
source

All Articles