I'm working on this tutorial, Getting Started with ASP.NET Core and Entity Framework Core using Visual Studio, and I'd like to deploy it to Azure from a GitHub repository. Using "Web App + SQL" in Azure I can deploy it successfully once, but when I make a change to the database and redeploy I get the error, "Applying existing migrations may resolve this issue..."
So is there some way I can trigger a database update when the app is redeployed with a new migration present?
Just a note, I know there are ways to deploy and manage an app like this directly from Visual Studio, but I'd like to learn how to do it from a repository.
Note: I found this similar question and I tried adding
context.Database.Migrate() to the Configure method of Startup.cs underneath
DbInitializer.Initialize(context); and this didn't cause a problem when I ran it on my machine but when I deployed it I got "500 Internal Server Error: An error occurred while starting the application."
I tried adding context.Database.Migrate() to the Configure method of Startup.cs underneath DbInitializer.Initialize(context); and this didn't cause a problem when I ran it on my machine but when I deployed it I got "500 Internal Server Error: An error occurred while starting the application."
According to your description, I followed the tutorial as you mentioned to test this issue. I added the
DbInitializer.cs in this section and add
context.Database.Migrate() under Configure method of
Startup.cs as follows:
I added a new property named
IdNo for Student class, then started the web application, I could encounter the following error:
Note: If the
Students table has no any records, the
DbInitializer.cs would add feed records, then I encounter the above error.
Here is the summary about
Ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created. If the database exists, then no effort is made to ensure it is compatible with the model for this context. Note that this API does not use migrations to create the database. In addition,the database that is created cannot be later updated using migrations. If you are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate() method to ensure the database is created and all migrations are applied.
Applies any pending migrations for the context to the database. Will create the database if it does not already exist. Note that this API is mutually exclusive with DbContext.Database.EnsureCreated(). EnsureCreated does not use migrations to create the database and therefore the database that is created cannot be later updated using migrations.
Based on your scenario, you need to leverage migrations feature to update your database schema. As I known, EF Core does not support automaic migrations, you could follow this similar case and this git issue. You could use
context.Database.Migrate() instead of
context.Database.EnsureCreated() in your
DbInitializer.cs and manually add migration file via
add-migration, and commit the created migration file to your GitHub repository.
Note: You need to take care of the seed data in
DbInitializer.cs, in order to avoid inserting the same record.