13

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

Yehia A.Salam
  • 1,987
  • 7
  • 44
  • 93
  • @tmg yup, attached another screenshot to the question to show the structure – Yehia A.Salam Jan 18 '15 at 23:08
  • @tmg i removed all the tables from the database, and built the SQL Server Project (added another screenshot), do i need to do anything else, e.g. EF migrations on top of that? – Yehia A.Salam Jan 18 '15 at 23:17
  • @tmg okay so i did the Enable-Migrations and Update-Database, and it works fine now. through im having a weird AspNet Users table with one column http://1drv.ms/14U52nR. But I'm planning to do a database first approach for the project, how do i use the database first approach to accomplish the same thing, thanks man. – Yehia A.Salam Jan 18 '15 at 23:30
  • @tmg okay fair enough, looks like EF7 is abandoning the edmx format, so ill go with the code first approach, can u move your comments in an answer so i can accept them thanks – Yehia A.Salam Jan 19 '15 at 23:02

2 Answers2

4

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

Community
  • 1
  • 1
tmg
  • 19,895
  • 5
  • 72
  • 76
  • @coderwill check the links for Database first approach. If they do not help you, please make new post – tmg Apr 07 '17 at 08:09
2

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);
AdrianBG
  • 21
  • 4