【问题标题】:Asp.Net Two Local DBContexts to One Azure DBContextAsp.Net 两个本地 DBContext 到一个 Azure DBContext
【发布时间】:2018-04-07 05:23:03
【问题描述】:

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

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 是两个 DBContext 的所有表,正如我所说,一切正常。我已经做了几十次迁移,只在我自己的 WebApp 表(MyDBContext)中

现在我想将字段添加到AplicationDBContext的表中,特别是AspNetUser表中,所以我修改了以下代码

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 迁移\ApplicationDbContext

Add-Migration -ConfigurationTypeName MyWebApp.Migrations.ApplicationDbContext.Configuration "AddFldAspNetUsers"

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

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

“/”应用程序中的服务器错误。 支持“ApplicationDbContext”上下文的模型在创建数据库后发生了变化。考虑使用 Code First 迁移来更新数据库

不知道怎么解决,求帮助:

【问题讨论】:

    标签: asp.net entity-framework azure entity-framework-migrations


    【解决方案1】:

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

    请遵循以下几点

    • 发布前,在发布窗口进入设置,在那里你可以 在 Databases 部分下找到 ApplicationDbContext。
    • 勾选复选框 Execute code first migrations(runs on 应用程序启动)选项。尝试重新发布它。 :)

    【讨论】:

    • 这就是解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    相关资源
    最近更新 更多