0
votes

ASP.NET WebAPI app published on Azure, Entity Framework code first.

After publishing with migrations executing on application start (first pic) I can't add new migration and work with database context ("model backing the 'DatabaseContext' context has changed since the database was created" exception). On adding new migration EF breaks with error:

Unable to generate an explicit migration because the following explicit migrations are pending: [migration name]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

Looks like EF doens't see migrations from __MigrationHistory table. I checked this table and last migrations exists.

In web.config there are only one connection string.

If it's important, DatabaseContextWithoutCache inherit DatabaseContext (they have different DbConfigurationType).

After adding null initializer Database.SetInitializer<DatabaseContext>(null); to database context it works but I still can't add any migration.

Why does Entity Framework doesn't see that migration is already appended to database? update-database breaks because there are already tables and columns in db.

EDIT:

In my migration list 2 migrations. I rolled back to init (first) migration and remove second. Then add-migration (now it contains changes from second and new changes) and run update-database. It works.

But EF ignore that db is up-to-date and breaks with "model backing the 'DatabaseContext' context has changed since the database was created" exception.

OMG I DON'T UNDERSTAND

first pic

2
If you don't care about the past migrations, tear everything down (remove table __MigrationHistory and migrations from code. Now enable-migrations, then create a new baseline: add-migration -BaselineName -IgnoreChanges followed by update-database. - Steve Greene
@SteveGreene I did it. And first 2 migrations were ok but 3rd braked. - Alexander Belenko

2 Answers

0
votes

I had this issue recently under VS 2013. If in the Package Manager Console you run the command add-migration {name} (optionally adding -startupprojectname {MyProjectName}) the EF should generate a new migration file for you. If that migration file has any content, then it believes you are not where you should be and you should then run update-database with that new migration in place.

Unfortunately it does seem possible to get caught in a loop with this (it happened to me) and there are quite a few SO Q&As which concern this. Very best of luck solving it.

0
votes

As the error suggests, there is a migration pending against the database.

Use the following command from the package manager (replace values with your own)

update-database -projectname yourprojectname -connectionstring "yourconnectionstring"

Then, create your new migration....

add-migration yournewmigrationname -projectname yourprojectname -connectionstring "yourconnectionstring"

And finally, run the newly created migration

update-database -projectname yourprojectname -connectionstring "yourconnectionstring"