[英]Deploying ASP.NET Core Web App + SQL from a repository
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. 我正在研究本教程《 使用Visual Studio的ASP.NET Core和Entity Framework Core入门》 ,我想将其从GitHub存储库部署到Azure。 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..."
在Azure中使用“ Web App + SQL”可以成功部署一次,但是当我对数据库进行更改并重新部署时出现错误,“应用现有迁移可以解决此问题...”
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. 请注意,我知道可以直接从Visual Studio部署和管理这样的应用程序,但是我想学习如何从存储库中进行操作。
Note: I found this similar question and I tried adding context.Database.Migrate()
to the Configure method of Startup.cs underneath DbInitializer.Initialize(context);
注意:我发现了类似的问题 ,我尝试将
context.Database.Migrate()
添加到DbInitializer.Initialize(context);
的Startup.cs的Configure方法中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." 当我在计算机上运行它时,这没有引起问题,但是当我部署它时,出现了“ 500 Internal Server Error:启动应用程序时发生错误”。
I tried adding context.Database.Migrate() to the Configure method of Startup.cs underneath DbInitializer.Initialize(context);
我尝试将context.Database.Migrate()添加到DbInitializer.Initialize(context)下的Startup.cs的Configure方法中; 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."
当我在计算机上运行它时,这没有引起问题,但是当我部署它时,出现了“ 500 Internal Server Error:启动应用程序时发生错误”。
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.cs
,并在Startup.cs
Configure方法下添加了context.Database.Migrate()
,如下所示:
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: 我为学生类添加了一个名为
IdNo
的新属性,然后启动了Web应用程序,可能会遇到以下错误:
Note: If the Students
table has no any records, the DbInitializer.cs
would add feed records, then I encounter the above error. 注意:如果“
Students
表没有任何记录,则DbInitializer.cs
将添加提要记录,然后遇到上述错误。
Here is the summary about DatabaseFacade.EnsureCreated()
and DatabaseFacade.Migrate()
: 这是有关
DatabaseFacade.EnsureCreated()
和DatabaseFacade.Migrate()
的摘要:
DatabaseFacade.EnsureCreated() 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 .
请注意,此API不使用迁移来创建数据库 。 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.
如果您以关系数据库为目标并使用迁移,则可以使用DbContext.Database.Migrate()方法来确保已创建数据库并应用了所有迁移。
DatabaseFacade.Migrate() 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() .
请注意, 此API与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.
sureCreated不使用迁移来创建数据库,因此创建的数据库以后不能使用迁移来更新。
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 .
如我所知,EF Core不支持自动迁移,您可以遵循这种类似情况和git 问题 。 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
使用context.Database.Migrate()
代替context.Database.EnsureCreated()
并通过add-migration
手动添加迁移文件,并将创建的迁移文件提交到GitHub存储库。
Note: You need to take care of the seed data in DbInitializer.cs
, in order to avoid inserting the same record. 注意:您需要注意
DbInitializer.cs
中的种子数据,以避免插入相同的记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.