如何在 Azure 部署的应用程序中应用数据库代码优先迁移? [英] How to apply database code first migrations in an azure deployed application?

查看:28
本文介绍了如何在 Azure 部署的应用程序中应用数据库代码优先迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将应用程序部署到 Azure 应用服务中.我的项目确实有要应用的迁移.但是我无法通过使用 azure app 命令行来应用它们

I just deployed an application into an azure App Service. My project does has migrations to be applied. But I was not able to apply them by using azure app command line

dotnet ef 数据库更新

dotnet ef database update

D:homesitewwwroot>没有找到匹配命令dotnet-ef"的可执行文件

D:homesitewwwroot> No executable found matching command "dotnet-ef"

我如何远程应用它们?

推荐答案

对于 dotnet-ef,我假设您在谈论 Entity Framework Core.我尝试在 KUDU 的 D:homesitewwwroot 和 VS 的 Package Manager Console 下运行 dotnet ef database update,我可能会遇到与您提到的相同的问题.

For dotnet-ef, I assumed that you are talking about Entity Framework Core. I have tried to run dotnet ef database update under D:homesitewwwroot of KUDU and Package Manager Console of VS, I could encounter the same issue as you mentioned.

我发现了一个类似的问题 没有找到匹配命令dotnet-ef"的可执行文件:

I found a similar issue No executable found matching command "dotnet-ef":

DotNet CLI 只能在项目目录中解析dotnet-ef".

DotNet CLI can only resolve "dotnet-ef" when it is within the project directory.

我可以通过 Powershell 运行命令 dotnet ef migrations adddotnet ef database update.详细的命令用法可以参考 EF Core .NET Command-line工具.

I could run the commands dotnet ef migrations add, dotnet ef database update via Powershell. For detailed command usage, you could follow EF Core .NET Command-line Tools.

如何在 Azure 部署的应用程序中应用数据库代码优先迁移?

How to apply database code first migrations in an azure deployed application?

根据我的理解,你不能那样做.您可能需要手动运行 update-database 作为 vivek nuna 在 VS 的包管理器控制台下回答,或者您可以使用 Powershell 和 cd 到您的项目目录,并执行 dotnet ef database update 将上下文的任何挂起迁移应用到数据库,然后将应用程序部署到 azure Web 应用程序.

Per my understanding, you could not do that. You may need to manually run update-database as vivek nuna answered under Package Manager Console of VS, or you could use Powershell and cd to your project directory, and execute dotnet ef database update to apply any pending migrations for your context to the database, then deploy your application to azure web app.

另外,EF 不支持自动迁移,您可能需要手动执行 Add-Migrationdotnet ef migrations add 来添加迁移文件.您可以显式执行命令来应用迁移,也可以在代码中应用迁移.你可以在Startup.cs文件的Configure方法中加入如下代码:

Additionally, EF does not support Automatic migrations, you may need to manually execute Add-Migration or dotnet ef migrations add for adding migration files. You could explicitly execute the command to apply the migrations, also you could apply migrations in your code. And you could add the following code in the Configure method of Startup.cs file:

using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
    scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
}

此外,您还可以关注这个类似的问题.

Moreover, you could also follow this similar issue.

这篇关于如何在 Azure 部署的应用程序中应用数据库代码优先迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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