Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 681
Yehia A.Salam Avatar asked Jan 18 '15 22:01

Yehia A.Salam


2 Answers

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

like image 174
tmg Avatar answered Nov 05 '22 03:11

tmg


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);
like image 2
AdrianBG Avatar answered Nov 05 '22 03:11

AdrianBG