Question

I'm trying to update my db with changes I've made in my entity models (a few extra table columns), but I don't want to drop & recreate the database as it will delete all my test data. Previously I've done this and used the Seed method to create some basic data, but now I have a lot of data which I'd rather keep.

I've tried to run update-database but I get the error:

There is already an object named 'ActivityNoteLines' in the database.

Looking at the migration script I can see that it's trying to create the table ActivityNoteLines. Obviously I don't want it to to that. I then created an initial migration using

Add-Migration InitialCreate –IgnoreChanges

but now when I run update-database, it thinks there are no changes!

I'm sure it's possible to do what I want, but I can't seem to get it to work. If I have to drop & recreate the db I will but would like to know if anyone has successfully updated AND kept the data.

Was it helpful?

Solution

The issue you are having is that your Create script in the Migrations folder wants to create all the tables. When Update runs it looks at the table dbo.MigrationHistory to see which migration scripts have already run. Your database doesn't have the create in the table so Update is trying to run it.

You can either try to manually add the entry to the table or edit the create script and erase the contents of the up and down methods. Then when you run update it will run the create script, which will do nothing, and write the entry to the MigrationHistory table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top