Deploying ASP.NET Core Web App + SQL from a repository

纵然是瞬间 提交于 2019-12-12 04:35:08

问题


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."


回答1:


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:

DbInitializer.Initialize(context);
context.Database.Migrate();

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 DatabaseFacade.EnsureCreated() and DatabaseFacade.Migrate():

DatabaseFacade.EnsureCreated()

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.

DatabaseFacade.Migrate()

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.



来源:https://stackoverflow.com/questions/42624340/deploying-asp-net-core-web-app-sql-from-a-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!