Yehia A.Salam
Yehia A.Salam

Reputation: 2028

Renaming dbo.AspNetUsers table

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

Upvotes: 13

Views: 15806

Answers (2)

AdrianBG
AdrianBG

Reputation: 21

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

Upvotes: 2

tmg
tmg

Reputation: 20413

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

Upvotes: 4

Related Questions