Faisal
Faisal

Reputation: 109

Entity Framework core getting error when rerunning the Update Migration

I am working on asp.net core and using EF. I have created database from scratch using EF. Now whenever there are changes in the database, adding new columns, or changing the type etc. Then I run the Update-datebase migration but I am getting an error for having tables and other object in database. Can someone help me what changes would I need to make in order to make the migration successful?

The compile error mentioned that the object already exists. Lets say if I have customer table that is already created in the database, when I run the script again after adding new object or modification, I am getting that error, this does make sense but what is the common practice to deal with the issue?

Upvotes: 1

Views: 564

Answers (1)

Yonatan Gross
Yonatan Gross

Reputation: 427

As described in Migrations Overview after you are evolving your models (adding properties, removing properties, editing them and etc...) you need to update your database schema, in order to do that, you are required to add a new migration.

This can be possible in the one of following ways:

  1. via .NET CLI

    dotnet ef migrations add NewMigrationName
    
  2. via PowerShell

    Add-Migration NewMigrationName
    

After adding a new migration you just need to sync the database by:

Update-Database

Upvotes: 1

Related Questions