daniel Snyder
daniel Snyder

Reputation: 68

Entity Framework core won't update database

I successfully created an Entity Framework Core migration and updated the database with it.

Then after I added another class, I created a second migration called "update1" which created a class of the same name from the command line tools. However, when I attempt to update the database, it fails. Here is the commands I used

dotnet ef migrations add update1  -c MyDbContext

dotnet ef database update update1  -c MyDbContext

and it failed with

There is already an object named MyTable in the database

which is a table which was created in the initial migration.

How can I tell it to either ignore the error, or else to only attempt to run the update1 migration?

Edit: deleting the table that was already there caused this odd behavior to stop happening and now it works as expected.

Thanks

Upvotes: 2

Views: 2522

Answers (2)

Lev K.
Lev K.

Reputation: 402

In my case I used Ubuntu, EF 6 and the database was in the docker and it's doesn't updated DB. I added --connection attribute and it's works, an example:

sudo dotnet ef database update --connection 
"Server=localhost;Database=temp;User Id=sa;Password=xxxxxxx;"

Upvotes: 1

Sampath
Sampath

Reputation: 65860

You have to remove the unwanted migration from the __MigrationHistory table.After that you can run your latest migration.This is happened due to you have manually deleted the table.B'cos EF doesn't know anything about your manual operations hence __MigrationHistory table still exist your old migration details (i.e. manually deleted table's record).

Upvotes: 3

Related Questions