Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have accidentally deleted data from __MigrationHistory table and I tried to make migration new tables and column changes to db. At this time EF6 show me an error

"There is already an object named 'xxxx' in the database.".
I tried the way by using this command and there is no effect on table column changes.

C#
Add-Migration MyMigration1 -IgnoreChanges
Update-database -Force

There are many recommendation about this for recreating db or another else. But those ways might be effect data losing to my db which contains a huge size of tested data and I can't able to lost any data. How to solve this error by without losing any data. If anybody please...

What I have tried:

I tried the way by using this command and there is no effect on table column changes.

Add-Migration MyMigration1 -IgnoreChanges
Update-database -Force
Posted
Updated 5-Sep-18 8:56am
v2

1 solution

Run Update-Database -Script -SourceMigration:0 to generate a SQL script for your database.

You'll then need to pick through the script to find the CREATE TABLE and INSERT statements for the _MigrationHistory table, and run just those statements against your database. NB: If you've only deleted the data, and the _MigrationHistory table still exists, you'll only need the INSERT statements.

If all of your databases are already up-to-date, you could also try the approach described by Rick Strahl:
Resetting Entity Framework Migrations to a clean Slate - Rick Strahl's Web Log[^]
But pay attention to the warning:
Quote:
Once the updates are applied your migration starting points are either no database at all, or the database in the fully updated base state. If you have databases that were a few iterations behind in migrations before you started the clean slate operations, there will be no easy way to get those in sync.


Once you've fixed the problem, back up your database and set up a scheduled job to keep it backed up. Next time you have a problem like this, you will simply be able to restore from the last good backup.
 
Share this answer
 
v2
Comments
Myo Zaw Latt 6-Sep-18 0:33am    
Thank you Richard,
I will try on your way

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900