Renaming dbo.AspNetUsers table

asp.net asp.net-identity entity-framework

Question

I'm trying to rename the default table names generated by ASP.net Identity 2.0. I read all the articles, the questions and the answers on stackoverflow but im still getting the same error.

I renamed the tables to Roles, UserClaims, Logins, UserRoles and Users. I also changed the application dbcontext to the following

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");

    }

}

But i keep getting the Invalid object name 'dbo.AspNetUsers'. error, and I have no idea why its still trying to locate AspNetUsers in the first place instead of just Users although i made the changes above. Totally desperate by now.

enter image description here

The database as well, same columns with the new table names:

enter image description here

And the SQL database project:

enter image description here

1
11
1/18/2015 11:16:59 PM

Accepted Answer

You need to update database. Enable-Migrations and Update-Database, explained in details here. The point of EF code first approach is to write our model classes and configurations and each time we change something we use EF migrations to update the database schema.

Database first approach with asp.net-identity-entityframework are explained here and here, not so straightforward

4
5/23/2017 12:10:22 PM

Popular Answer

Write the following code in IdentityModels.cs

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();
    }
}

Write the following code in Application_Start() Method in Global.asax.cs file

Database.SetInitializer<ApplicationDbContext>(null);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow