从存储库部署ASP.NET Core Web App + SQL [英] Deploying ASP.NET Core Web App + SQL from a repository

查看:126
本文介绍了从存储库部署ASP.NET Core Web App + SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究本教程,入门使用Visual Studio的ASP.NET Core和Entity Framework Core ,我想将其从GitHub存储库部署到Azure.在Azure中使用"Web App + SQL"可以成功部署一次,但是当我对数据库进行更改并重新部署时出现错误,应用现有迁移可以解决此问题..."

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?

仅需注意,我知道有很多方法可以直接从Visual Studio部署和管理这样的应用程序,但是我想学习如何从存储库中进行操作.

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.

注意:我发现了这个类似的问题,我尝试将context.Database.Migrate()添加到DbInitializer.Initialize(context);下的Startup.cs的Configure方法中,这在我在计算机上运行它时并没有引起问题,但是当我部署它时,出现了"500 Internal Server Error :启动应用程序时发生错误."

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

推荐答案

我尝试将context.Database.Migrate()添加到DbInitializer.Initialize(context)下的Startup.cs的Configure方法中;并且当我在计算机上运行它时,这没有引起任何问题,但是当我部署它时,出现了"500 Internal Server Error:启动应用程序时发生错误."

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

根据您的描述,我遵循了教程如您所提到的,用于测试此问题.我在DbInitializer.cs -with-test-data"rel =" nofollow noreferrer>部分,并在Startup.cs的Configure方法下添加context.Database.Migrate(),如下所示:

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();

我为Student类添加了一个名为IdNo的新属性,然后启动了Web应用程序,我可能会遇到以下错误:

I added a new property named IdNo for Student class, then started the web application, I could encounter the following error:

注意:如果Students表中没有任何记录,则DbInitializer.cs将添加供稿记录,那么我会遇到上述错误.

Note: If the Students table has no any records, the DbInitializer.cs would add feed records, then I encounter the above error.

以下是有关DatabaseFacade.EnsureCreated()DatabaseFacade.Migrate()的摘要:

DatabaseFacade.EnsureCreated()

确保上下文数据库存在.如果存在,则不执行任何操作 .如果它不存在,那么将创建数据库及其所有架构. 如果数据库存在,则不做任何努力以确保它与以下数据库兼容 此上下文的模型. 请注意,此API不使用迁移来创建数据库.此外,创建的数据库以后无法使用迁移进行更新. 如果您以关系数据库为目标并使用迁移,则可以使用DbContext.Database.Migrate()方法来确保已创建数据库并应用了所有迁移.

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()

将上下文的所有未决迁移应用到数据库.如果尚不存在,将创建数据库.请注意,此API与DbContext.Database.EnsureCreated()是互斥的. 确保创建不使用迁移来创建数据库,因此 创建的数据库以后无法使用迁移来更新.

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.

根据您的方案,您需要利用迁移功能来更新数据库架构.众所周知,EF Core不支持自动迁移,您可以按照类似的 case 进行操作, git 问题.您可以在DbInitializer.cs中使用context.Database.Migrate()代替context.Database.EnsureCreated(),并通过add-migration手动添加迁移文件,并将创建的迁移文件提交到GitHub存储库中.

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.

注意:您需要注意DbInitializer.cs中的种子数据,以避免插入相同的记录.

Note: You need to take care of the seed data in DbInitializer.cs, in order to avoid inserting the same record.

这篇关于从存储库部署ASP.NET Core Web App + SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆