EF未创建AspNet标识表



我有一个简单的aspnet Identity模式,但我无法在数据库中创建Identity表。以下是MVC网站用于标识的主要类:

 public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        if (modelBuilder == null)
        {
            throw new ArgumentNullException("modelBuilder");
        }
        modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");
        EntityTypeConfiguration<ApplicationUser> table =
            modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");
        table.Property((ApplicationUser u) => u.UserName).IsRequired();
        modelBuilder.Entity<ApplicationUser>().HasMany<IdentityUserRole>((ApplicationUser u) => u.Roles);
        modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) =>
            new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");
        EntityTypeConfiguration<IdentityUserLogin> entityTypeConfiguration =
            modelBuilder.Entity<IdentityUserLogin>().HasKey((IdentityUserLogin l) =>
                new
                {
                    UserId = l.UserId,
                    LoginProvider = l.LoginProvider,
                    ProviderKey
                        = l.ProviderKey
                }).ToTable("AspNetUserLogins");
        entityTypeConfiguration.HasRequired<IdentityUser>((IdentityUserLogin u) => u.User);
        EntityTypeConfiguration<IdentityUserClaim> table1 =
            modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
        table1.HasRequired<IdentityUser>((IdentityUserClaim u) => u.User);
        modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");
        EntityTypeConfiguration<IdentityRole> entityTypeConfiguration1 =
            modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");
        entityTypeConfiguration1.Property((IdentityRole r) => r.Name).IsRequired();
    }
}

当我尝试登录时,会执行OnModelCreating方法,但当我转到数据库时,没有创建表,并且我得到错误"无效列名AspNetUsers"。这里怎么了?

到您的Global。Asax添加

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());

这将在每次模型更改时删除并重新创建数据库。

阅读有关实体框架数据库初始化策略的更多信息

在我的测试开发环境中:

移除桌子"__迁移历史";

出于某种原因,它阻止了其他表的创建。

相关内容

  • 没有找到相关文章

最新更新