ASP.NET Core Publish to Azure因数据库更新而失败

时间:2018-06-14 16:51:07

标签: azure asp.net-core publish

使用数据库迁移发布ASP.NET Core Web应用程序失败,并在Visual Studio中出现以下错误

Web deployment task failed. (Make sure the database connection string for the server is correct and that you have appropriate permission to access the database. (Web Deploy Provider is "dbFullSql").
Error details:Could not complete an operation with the specified provider ("dbFullSql") when connecting using the Web Management Service. This can occur if the server administrator has not authorized the user for this operation. dbFullSql http://go.microsoft.com/fwlink/?LinkId=178034  Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_USER_NOT_AUTHORIZED_FOR_DBFULLSQL.)

从cmd行:

"C:\Dev\Neptune.Web\Neptune.Web.csproj" (default target) (1) ->
(MSDeployPublish target) ->
  C:\Program Files\dotnet\sdk\2.1.300\Sdks\Microsoft.NET.Sdk.Publish\build\netstandard1.0\PublishTargets\Microsoft.NET.Sdk.Publi
sh.MSDeploy.targets(139,5): error : Web deployment task failed. (Make sure the database connection string for the server is corr
ect and that you have appropriate permission to access the database. (Web Deploy Provider is "dbFullSql"). [C:\Dev\Neptune.Web\Neptune.Web.csproj]
C:\Program Files\dotnet\sdk\2.1.300\Sdks\Microsoft.NET.Sdk.Publish\build\netstandard1.0\PublishTargets\Microsoft.NET.Sdk.Publish
.MSDeploy.targets(139,5): error : Error details:Could not complete an operation with the specified provider ("dbFullSql") when c
onnecting using the Web Management Service. This can occur if the server administrator has not authorized the user for this oper
ation. dbFullSql http://go.microsoft.com/fwlink/?LinkId=178034  Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERR
OR_USER_NOT_AUTHORIZED_FOR_DBFULLSQL.) [C:\Dev\Neptune.Web\Neptune.Web.csproj]

    0 Warning(s)
    1 Error(s)

如果我注释掉本地数据库连接字符串并使用连接字符串直接将更新应用于Azure SQL数据库(即直接在代码中连接到数据库),这就是奇怪的事情:#/ p>

C:\Dev\Neptune.Web>dotnet ef database update
Applying migration '20180613012042_CreateDate'.
Done.

我做过/检查的事情:

  1. Azure数据库服务器防火墙设置在允许列表中显示我的客户端IP地址
  2. 我可以在Visual Studio中连接到数据库并查看表格。应用数据库更新后,我可以看到对架构的更改
  3. 我尝试从VS内部和CL中发布,并在应用数据库迁移时获得相同的结果。如果没有迁移或未选中“应用迁移”复选框,则发布工作正常。
  4. db用户是我为数据库创建的唯一用户,使用该用户的所有其他交互工作正常。
  5. 这在过去是有效的。不确定它是否在升级到2.1或之前停止,但最近才开始失败。

1 个答案:

答案 0 :(得分:0)

根据你的描述,它太奇怪了。 以下是您可以参考的migratie existing asp.net core 2.0 application to asp.net core 2.1文章。

Managing database schema and seeding data with EF core migrations

如果您想要apply migration at runtime,可以在Startup.cs的配置方法中添加以下代码。

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

有关详细信息,请参阅此thread