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

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

问题描述

我刚刚将应用程序部署到了Azure App Service中。
我的项目确实要应用迁移。
但是我无法通过使用azure应用程序命令行来应用它们

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:\home\site\wwwroot>找不到与命令 dotnet-ef匹配的可执行文件

D:\home\site\wwwroot> No executable found matching command "dotnet-ef"

如何远程应用它们?

推荐答案

对于 dotnet-ef ,我假设您正在谈论Entity Framework Core。我试图在KUDU的 D:\home\site\wwwroot 下运行 dotnet ef数据库更新 VS的Package Manager Console,我可能遇到与您提到的相同的问题。

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

我发现了类似的问题找不到与命令 dotnet-ef匹配的可执行文件


DotNet CLI仅在项目目录中时才能解析 dotnet-ef。

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

我可以运行命令 dotnet ef迁移通过Powershell添加 dotnet ef数据库更新。有关命令的详细用法,您可以遵循 EF Core .NET命令-行工具

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.


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

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

根据我的理解,您无法做到这一点。您可能需要手动运行更新数据库作为VS的Package Manager控制台下的vivek nuna回答,或者可以使用Powershell和cd到项目目录,然后执行 dotnet ef数据库更新,以便将上下文的任何待处理迁移应用到数据库,然后将应用程序部署到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不支持自动迁移,您可能需要手动执行添加迁移 dotnet ef迁移添加添加迁移文件。您可以显式执行命令以应用迁移,也可以在代码中应用迁移。您可以在 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天全站免登陆