重命名dbo.AspNetUsers表

时间:2015-01-18 22:56:19

标签: asp.net entity-framework asp.net-identity

我正在尝试重命名ASP.net Identity 2.0生成的默认表名。我在stackoverflow上阅读了所有文章,问题和答案,但我仍然得到同样的错误。

我将表重命名为Roles,UserClaims,Logins,UserRoles和Users。我还将应用程序dbcontext更改为以下

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

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

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


        modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles", "dbo");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims", "dbo");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins", "dbo");

    }

}

但我一直收到Invalid object name 'dbo.AspNetUsers'.错误,我不知道为什么它仍然试图首先找到 AspNetUsers ,而不仅仅是用户我做了上面的修改。完全绝望了。

enter image description here

数据库以及具有新表名的相同列:

enter image description here

SQL数据库项目:

enter image description here

2 个答案:

答案 0 :(得分:4)

您需要更新数据库。 Enable-MigrationsUpdate-Database,详细解释here。 EF代码第一种方法的要点是编写我们的模型类和配置,每次我们改变某些东西时,我们都使用EF迁移来更新数据库模式。

使用asp.net-identity-entityframework的数据库第一种方法解释为herehere,而不是那么简单

答案 1 :(得分:0)

在IdentityModels.cs中编写以下代码

resource "aws_subnet" "myapp" {
  vpc_id     = "${aws_vpc.main.id}"
  cidr_block = "${cidrsubnet(lookup(var.vpc_cidr[random_shuffle.vpc_cidr.result], terraform.workspace), 5, count.index + 16 + 5)}"
}

resource "random_shuffle" "vpc_cidr" {
  input = ["vpc_cidr", "vpc_cidr_2"]
}

variable "vpc_cidr" {
  type = "map"

  default = {
    vpc_cidr = {
      "QA"   = "20.30.100.0/23"
      "TEST" = "20.37.200.0/23"
      "PROD" = "20.37.200.0/23"
      "DEV"  = "20.37.100.0/23"
    }

    vpc_cidr_2 = {
      "QA"   = "10.30.182.0/23"
      "TEST" = "10.37.238.0/23"
      "PROD" = "<none>"
      "DEV"  = "<none>"
    }
  }
}

在Global.asax.cs文件的Application_Start()方法中编写以下代码

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

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

        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
        modelBuilder.Entity<ApplicationUser>().ToTable("Users");
    }

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