EF Migration object already exists error

37,032

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) :

  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.

Share:
37,032

Related videos on Youtube

Arianit
Author by

Arianit

Updated on May 14, 2020

Comments

  • Arianit
    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
    Arianit almost 8 years
    is there any way that syncs the models if I make changes in the database in the SqlServer Managenment Sdutio?
  • Amr Alaa
    Amr Alaa almost 8 years
    I think then you will need to regenerate the models like you do in database first approach
  • Muhammad Younus
    Muhammad Younus over 7 years
    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 ;).
  • user692942
    user692942 over 6 years
    Careful you haven't just mistyped the database name in the connection string before you start doing this.
  • Maxime
    Maxime about 6 years
    It worked for me, but with dotnet ef database update
  • Danish Shaikh
    Danish Shaikh about 6 years
    @Maxime thank you i updated the Answer was silly mistake
  • mavi
    mavi almost 6 years
    Enable-Migrations is obsolete.
  • Lutaaya Huzaifah Idris
    Lutaaya Huzaifah Idris almost 5 years
    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 ?
  • Mayer Spitz
    Mayer Spitz about 4 years
    How can I recover previous migrations?
  • Peyman Majidi
    Peyman Majidi over 3 years
    Your answer is about DotNetCore, but he/she is looking for ASP.NET MVC FW
  • amr ras
    amr ras about 3 years
    Add-Migration : A parameter cannot be found that matches parameter name 'IgnoreChanges'.