Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Migration object already exists error

Tags:

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?

like image 700
Arianit Avatar asked Jun 30 '16 16:06

Arianit


People also ask

What is __ Efmigrationshistory?

By default, EF Core keeps track of which migrations have been applied to the database by recording them in a table named __EFMigrationsHistory . For various reasons, you may want to customize this table to better suit your needs.


1 Answers

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

like image 144
Amr Alaa Avatar answered Oct 16 '22 02:10

Amr Alaa