ASP.NET Migration期望AspNetUsers即使在OnModelCreate中重命名

时间:2016-07-24 03:52:42

标签: asp.net asp.net-mvc

我正在尝试重命名我的Identity 2.0表,让我的应用名称在他们面前。所以我已经覆盖了OnModelCreating:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("appname_Users");
        modelBuilder.Entity<IdentityRole>().ToTable("appname_Roles");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("appname_UserClaims");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("appname_UserLogins");
        modelBuilder.Entity<IdentityUserRole>().ToTable("appname_UserRoles");
    }

我删除了Migrations文件夹,确保这些表不存在,然后运行:

enable-migrations -Force
update-database

当我尝试登录该网站时,仍然说它找不到dbo.AspNetUsers。

当我检查迁移脚本时,我看到以下内容:

  CreateTable(
            "dbo.AspNetUsers",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.appname_Users", t => t.Id)
            .Index(t => t.Id);

它甚至认为它需要AspNetUsers?我已经搜索了文档,但无法找出发生了什么。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

手动从数据库中删除所有身份表。然后在onModelCreating insert

        modelBuilder.Entity<ApplicationUser>().ToTable("appname_Users");
        modelBuilder.Entity<IdentityUserRole>().ToTable("appname_UserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("appname_UserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("appname_UserClaims");
        modelBuilder.Entity<IdentityRole>().ToTable("appname_Roles");

如果您创建自定义标识类,请使用

        modelBuilder.Entity<ApplicationUser>().ToTable("appname_Users");
        modelBuilder.Entity<ApplicationRole>().HasKey<string>(r => r.Id).ToTable("appname_Roles");
        modelBuilder.Entity<ApplicationUser>().HasMany<ApplicationUserRole>((ApplicationUser u) => u.UserRoles);
        modelBuilder.Entity<ApplicationUserRole>().HasKey(r => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("appname_UserRoles");

这种方式对我有用.........