【问题标题】:Why is my Extended Identity Account failing to automigrate to the AspNetUsers table?为什么我的扩展身份帐户无法自动迁移到 AspNetUsers 表?
【发布时间】:2015-01-29 17:10:02
【问题描述】:

我在“开箱即用”ApplicationUser 类中添加了一个名为 ResetPassword 的新属性,但是当我运行“add-migration”时,自动生成的迁移类中的 up/down 方法是空白的,当我尝试登录时应用程序引发以下错误“支持 'ApplicationDbContext' 上下文的模型自创建数据库以来已更改。考虑使用 Code First 迁移来更新数据库”。

这是我的代码:

    public class ApplicationUser : IdentityUser
{
    public bool ResetPassword { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {

    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

可能 DAL 类如下所示:

public class DefaultConnection : DbContext
{
    public DefaultConnection()
        : base("DefaultConnection")
    {
        this.Configuration.ProxyCreationEnabled = false;
        this.Configuration.LazyLoadingEnabled = false;
    }

    public DbSet<Category> Categories { get; set; }
    public DbSet<Group> Groups { get; set; }
    public DbSet<Model> Models { get; set; }
    public DbSet<Variable> Variables { get; set; }
    public DbSet<Column> Columns { get; set; }
    public DbSet<Project> Projects { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Config> Configs { get; set; }
    public DbSet<Target> Targets { get; set; }
    public DbSet<Organisation> Organisations { get; set; }
    public DbSet<OrgGroup> OrgGroups { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }              
}

登录时,这是引发错误的行:

var userManager = context.OwinContext.GetUserManager();

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5 entity-framework-migrations asp.net-identity-2


    【解决方案1】:

    您的应用程序中有 2 个上下文。运行迁移时,您需要指定要迁移的上下文类型:

    Add-Migration -configuration <Namespace>.ApplicationDbContext <Migrations-Name>
    

    请参阅this page,了解有关在同一程序集中使用具有多个 DbContext 的迁移的更多详细信息。

    【讨论】:

      猜你喜欢
      • 2018-08-17
      • 2016-08-17
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2019-02-17
      • 2021-12-08
      相关资源
      最近更新 更多