Asp.Net 两个本地 DBContext 到一个 Azure DBContext

Asp.Net Two Local DBContexts to One Azure DBContext

我有一个通过本教程开发的网络应用程序:

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview

本教程使用 EF 和 Code First 将两个 DBContext(ApplicationDbContext 和 MyDBContext)与它们各自的数据库放在一起,几个月前我在 Azure 中发布了它,无论是在本地还是在 Azure 中,一切都运行良好。从一开始我就注意到 Azure 只管理一个数据库。在这个数据库中,Azure 是两个 DBContexts 的所有 tables,正如我所说,一切正常。我做了几十次迁移,只在我自己的 WebApp tables (MyDBContext)

现在我想添加字段到AplicationDBContext的一个table,具体到AspNetUsertable,所以我修改下面的代码

public class ApplicationUser: IdentityUser
{
    /// My New Field
    public bool Disabled {get; set; }

    public ClaimsIdentity GenerateUserIdentity (ApplicationUserManager manager)
    {
        var userIdentity = manager.CreateIdentity (this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    public Task <ClaimsIdentity> GenerateUserIdentityAsync (ApplicationUserManager manager)
    {
        return Task.FromResult (GenerateUserIdentity (manager));
    }
}

然后我实现了:

Enabled-Migrations -ContextTypeName ApplicationDbContext -MigrationsDirectory Migrations \ ApplicationDbContext

添加迁移 -ConfigurationTypeName MyWebApp.Migrations.ApplicationDbContext.Configuration "AddFldAspNetUsers"

更新数据库-ConfigurationTypeName MyWebApp.Migrations.ApplicationDbContext.Configuration

在本地一切正常,但是当我在 Azure 中发布时出现以下错误:

“/”应用程序中的服务器错误。 支持 'ApplicationDbContext' 上下文的模型自数据库创建以来已更改。考虑使用代码优先迁移更新数据库

不知道怎么解决,求助:

我最近遇到了同样的问题。问题背后的原因是您搭建了迁移的脚手架,但尚未更新 Azure 中的身份数据库表。

请遵循以下几点

  • 发布之前,在发布中window进入设置,在那里你可以 在数据库部分下找到 ApplicationDbContext。
  • 勾选先执行代码迁移复选框(运行于 应用程序启动)选项。尝试重新发布它。 :)