EF Migration object already exists error
Solution 1
try to Run the
Add-Migration InitialCreate –IgnoreChanges
command in Package Manager Console. This creates an empty migration with the current model as a snapshot. and then Run the
Update-Database
command in Package Manager Console. This will apply the InitialCreate migration to the database. Since the actual migration doesn’t contain any changes, it will simply add a row to the __MigrationsHistory table indicating that this migration has already been applied.
see this
then change your models and add migration.
another approach is to simply comment all the code on up and down methods
Solution 2
Best and working For me idea is to comment all the code in UP and Down functions of Initial migration file and then fire
dotnet ef database update
this should work fine,make sure you update migration before commenting out initial migration
Solution 3
If none of those answers work for you, you may check if in your code you use the method
context.Database.EnsureCreated()
Be aware that that method doesn't apply any migrations (source) (you can check this making a sql query to the database and like "select * from __EfMigrationHistory" and be sure that there are no migrations on that database.
EF provide a really good method to help this that is
context.Database.Migrate()
that no only it will ensure that you have a database, but also use migrations and migrate yout db to the last version.
Hope it helps
Solution 4
This error appears when you deleted previous migrations and your new migration try to create new table that already exist. Recover previous migration and everything will be ok.
Solution 5
Amr Alaa solution works, but the database didn't migrate to the latest model.
Here's how it worked (using automatic migration) :
- Delete Migrations folder
- Execute enable-migrations
Set this two properties to true in the newly created Configuration.cs
public Configuration() { AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; }
Execute Update-Database -Force
Your database will be updated to latest scheme and ready.
hope this helps.
Related videos on Youtube
Arianit
Updated on May 14, 2020Comments
-
Arianit about 4 years
I am working on an ASP.NET MVC project with Entity Framework with code first from database. I get the models for each table in the database. I made some changes in the models, enabled migrations and when I initial the migration I get an error:
There is already an object named 'TableName' in the database."
I tried with
update-database -force
but didn't help. The initial migration creates the tables that already exist!How to make the initial migration apply the changes on the models and not create the tables from beginning?
And what is the best practice to sync changes between database and models in this case?
-
Arianit almost 8 yearsis there any way that syncs the models if I make changes in the database in the SqlServer Managenment Sdutio?
-
Amr Alaa almost 8 yearsI think then you will need to regenerate the models like you do in database first approach
-
Muhammad Younus over 7 yearsYup @Arianit simple remove the connection string remove the context model regenerate classes with same context name using code first model from database and u r good to go ;).
-
user692942 over 6 yearsCareful you haven't just mistyped the database name in the connection string before you start doing this.
-
Maxime about 6 yearsIt worked for me, but with
dotnet ef database update
-
Danish Shaikh about 6 years@Maxime thank you i updated the Answer was silly mistake
-
mavi almost 6 yearsEnable-Migrations is obsolete.
-
Lutaaya Huzaifah Idris almost 5 yearsDo you need to create your own constructor on this e.g
Configuration
?, even theAutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
are not resolved, how can these be resolved ? -
Mayer Spitz about 4 yearsHow can I recover previous migrations?
-
Peyman Majidi over 3 yearsYour answer is about
DotNetCore
, but he/she is looking forASP.NET MVC
FW -
amr ras about 3 yearsAdd-Migration : A parameter cannot be found that matches parameter name 'IgnoreChanges'.