24

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?

5 Answers 5

34

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

Sign up to request clarification or add additional context in comments.

5 Comments

is there any way that syncs the models if I make changes in the database in the SqlServer Managenment Sdutio?
I think then you will need to regenerate the models like you do in database first approach
Yup @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 ;).
Careful you haven't just mistyped the database name in the connection string before you start doing this.
Add-Migration : A parameter cannot be found that matches parameter name 'IgnoreChanges'.
6

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

2 Comments

It worked for me, but with dotnet ef database update
@Maxime thank you i updated the Answer was silly mistake
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

1 Comment

Your answer is about DotNetCore, but he/she is looking for ASP.NET MVC FW
2

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.

1 Comment

How can I recover previous migrations?
1

Amr Alaa solution works, but the database didn't migrate to the latest model.

Here's how it worked (using automatic migration) :

  1. Delete Migrations folder
  2. Execute enable-migrations
  3. Set this two properties to true in the newly created Configuration.cs

    public Configuration() { AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; } 
  4. Execute Update-Database -Force

Your database will be updated to latest scheme and ready.

hope this helps.

2 Comments

Enable-Migrations is obsolete.
Do you need to create your own constructor on this e.g Configuration?, even the AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; are not resolved, how can these be resolved ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.