我刚刚将一个应用程序部署到 Azure 应用服务中。 我的项目确实有要应用的迁移。 但我无法使用 azure app 命令行应用它们
dotnet ef database update
D:\home\site\wwwroot> No executable found matching command "dotnet-ef"
如何远程应用它们?
最佳答案
对于 dotnet-ef
,我假设您正在谈论 Entity Framework Core。我尝试在KUDU的D:\home\site\wwwroot
和VS的Package Manager Console下运行dotnet ef database update
,我可能会遇到与你提到的相同的问题.
我发现了类似的问题No executable found matching command "dotnet-ef" :
DotNet CLI can only resolve "dotnet-ef" when it is within the project directory.
我可以通过 Powershell 运行命令 dotnet ef migrations add
、dotnet ef database update
。详细命令使用方法可以关注EF Core .NET Command-line Tools .
How to apply database code first migrations in an azure deployed application?
根据我的理解,你不能这样做。您可能需要按照 vivek nuna 在 VS 的 Package Manager Console 下回答的方式手动运行 update-database
,或者您可以使用 Powershell 并 cd 到您的项目目录,然后执行 dotnet ef database update
将上下文的任何挂起的迁移应用到数据库,然后将应用程序部署到 Azure Web 应用程序。
此外,EF不支持自动迁移,您可能需要手动执行Add-Migration
或dotnet ef migrations add
来添加迁移文件。您可以显式执行命令来应用迁移,也可以在代码中应用迁移。您可以在 Startup.cs
文件的 Configure
方法中添加以下代码:
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
}
此外,您还可以关注类似的issue .
关于.net - 如何在 Azure 部署的应用程序中应用数据库代码优先迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50243829/