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

标签 .net azure ef-code-first dotnet-cli

我刚刚将一个应用程序部署到 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 adddotnet 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-Migrationdotnet 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/

相关文章:

c# - 如何管理 WinForms 应用程序的大型界面?

c# - 如果第一个为假,编译器是否会继续计算一个表达式,其中所有表达式都必须为真?

iis - 如果更新虚拟机大小,Azure 虚拟机软件会发生什么情况?

entity-framework - 我们如何在 Entity Framework 7 上配置约定?

.net - 将 IEnumerable 变量从 ColdFusion 传递到 .NET

.net - HttpContext.Current的生命周期是多长?

reactjs - 我正在使用 React-adal 进行 Azure AD 单点登录。它的 token 将在 1 小时后过期。有没有办法刷新 session 或延长 session 过期时间

c# - 绑定(bind)中的Azure函数表存储违反了类型参数 'TElement'的约束

entity-framework - Entity Framework 代码优先和无效对象名称错误

entity-framework - Entity Framework 6 : How to override SQL generator?