1

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.

5
  • what database initializer are you using? Commented Apr 24, 2014 at 12:47
  • @Thewads - I am using CreateDatabaseIfNotExists, having previously used DropCreateDatabaseIfModelChanges which obviously was deleting all my data. Commented Apr 24, 2014 at 12:49
  • 1
    if you are using code-first migrations, look at using MigrateDatabaseToLatestVersion Commented Apr 24, 2014 at 13:22
  • Can you just delete everything from the migration that you don't want? Commented Apr 24, 2014 at 13:56
  • @Dismissile - I probably could, but I thought there might be a nicer way of doing it Commented Apr 24, 2014 at 14:00

1 Answer 1

2

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.