Entity framework manually deleted tables cant be generated from EF migration
B

5

12

I have created migration and created the database and the tables . For example the tables are

A B C D E . Now again I have changed some part of code and ran update-database command . Everything went smooth and nice and the tables showed the columns . Now accidentally I manually deleted one two tables D and E. Now when I try to run the migration with update-database . It runs properly but doesn't create the tables which I deleted manually . I tried to delete the existing migration and re-run update-database . It gives the error that apart from the two tables . There already an object existing in 'A ,B, C ' bla bla name.

Any idea how to get rid of this situation without dropping database and recreate the deleted tables using migration ? Cause i dont want to drop the database as it contains the data in rest of the tables. How to proceed in this situation where I have existing tables in database and accidentally I have manually deleted few tables from SQL server from SSMS .

How to recreate the tables again using migration ?

Oh my entity framework version is 6.0.2

Belita answered 1/2, 2014 at 7:50 Comment(0)
B
2

I finally figured out the solution . Its basically change in strategy how we use migration . The Migration Add-migration only checks the Models and the previous migration timestamp cs files possibly . so until and unless we provide update-database command in nuget . it Never actually gets to know which Tables have got deleted manually . So to the context when we try to perform some migration like alter on the tables . It causes problematic migration since that table doesn't exist on the database but the migration assumes it is already there . So for that there is a manual work . Here are the steps after we have deleted a table manually from the database

  1. Check the models existing in entities with correspondence to the database table . If we have found there are some anomaly in the database table which is missing from database but it exists as entity that means. They both are not in sync with each other. So we have to find the model => Table relation for each .

  2. If we have created initial migration with all tables then copy the deleted createTable code from the migration file .

  3. Paste it into the last recently generated migration file. And then generate the script or run the update-database command . That would create the deleted database table . However there is no automatic command which would both sync between all entities and the database tables . That's something which we have to track partially manual in migration.

Belita answered 27/2, 2014 at 19:42 Comment(0)
L
5

EF Migration history is stored in the table _MigrationHistory. Remove the table from the Database. Caution: This will erase all the Migration history and you will have to re-create all the tables

Laky answered 20/1, 2017 at 8:39 Comment(1)
I know it's old, but not in every case you should delete whole table. Remove only migration row u need to reapply, database will think its not applied and it will auto update - you don't even need to manually enter command Update-Database. Keep in mind its safe only if u are working on last applied migration, otherwise data inconsistency might happen.Alanaalanah
R
4

you can do a simple trick here, remove the specific migration history rows, which affect your deleted tables from

dbo.__EFMigrationsHistory

then run update-database command, enjoy

Radu answered 18/5, 2020 at 8:41 Comment(1)
the best solution as usual got no points.. well done anyway!Raquelraquela
B
2

I finally figured out the solution . Its basically change in strategy how we use migration . The Migration Add-migration only checks the Models and the previous migration timestamp cs files possibly . so until and unless we provide update-database command in nuget . it Never actually gets to know which Tables have got deleted manually . So to the context when we try to perform some migration like alter on the tables . It causes problematic migration since that table doesn't exist on the database but the migration assumes it is already there . So for that there is a manual work . Here are the steps after we have deleted a table manually from the database

  1. Check the models existing in entities with correspondence to the database table . If we have found there are some anomaly in the database table which is missing from database but it exists as entity that means. They both are not in sync with each other. So we have to find the model => Table relation for each .

  2. If we have created initial migration with all tables then copy the deleted createTable code from the migration file .

  3. Paste it into the last recently generated migration file. And then generate the script or run the update-database command . That would create the deleted database table . However there is no automatic command which would both sync between all entities and the database tables . That's something which we have to track partially manual in migration.

Belita answered 27/2, 2014 at 19:42 Comment(0)
T
1

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

Update-Database -Source MigrationBeforeCreatingTables -Target MigrationAfterCreatingTables -Script
Tele answered 2/2, 2014 at 9:6 Comment(2)
But I manually deleted the tables from database. Not from model. The models remain the same . The migrationAftercreatingTables when I try to generate it doesnt give the tables . hence it doesnt have any migration related stuff . Empty UP and Down methods are generated.Belita
With MigrationAfterCreatingTables I meant the migration, where the tables were originally created. Alternatively you can use Update-Database -Source 0 -Script command, that generates SQL scripts for all migrations.Tele
W
0

not best but possible way: 1)create a new migration, don't update the base 2)find migration that added tables you need 3)copy up and down parts from that old migration 4)past those things in the new migration file 5)update the database by the new migration

Wantage answered 29/11, 2021 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.