0

I have a C# project where I am using Entity Framework 6.1 to manage the database. In the process of development I wanted to add some more fields to some of the tables. So I edited the InitialCreate.cs migration to include the new fields and then deleted my database within SQL Server. When I go to run Update-Database I get the following error ...

Unable to update database to match the current model because there are pending changes and automatic migration is disabled

Since I had deleted the database from the server I didn't expect to get this error. There is no more migration table so I expected the modified version of the InitialCreate.cs file to be used to create the database. I am trying to avoid having multiple migrations in deference to a single InitialCreate.cs file. Why isn't the application just using the InitialCreate.cs file that have been editing directly? Why does it need to create a new migration file?

I know I could just use Add-Migration in the package manager console but I don't want multiple migration files. I just want one. I thought with code-first I could do this.

1 Answer 1

2

In code first you should make all your changes in your models and let that update your database via initializers or migrations. If you want to minimize the number of migration files you can always delete (roll up) them and reset your baseline:

1) Delete all your migrations

2) Add a no-op migration using add-migration MyStartingPoint -IgnoreChanges. This will take a snapshot of the current state of your models with no code in Up()/Down(). update-database to apply.

3) Now make all the model changes you need and add a new migration which will just be the new stuff.

At any point you could delete all the migrations and repeat this step so long as all your deployed databases are up to date. You could also just delete the last migration and re-add it and it will pickup all the differences from the prior migration. See here.

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.