3

This is similar to this question but with using database first entity framework instead of code first: How can I change the table names when using Visual Studio 2013 ASP.NET Identity?

In the code first approach you can override OnModelCreating and do something like this so that the user info is saved in MyUsers table instead of the default AspNetUsers:

modelBuilder.Entity<IdentityUser>().ToTable("MyUsers")

In the database first approach I have manually created the MyUsers table but because OnModelCreating does not get called I don't know how to configure the data to be saved into my new table?

Community
  • 1
  • 1
Bob
  • 4,236
  • 12
  • 45
  • 65

1 Answers1

0

You can follow the Code First approach an when overriding the OnModelCreating add the line:

System.Data.Entity.Database.SetInitializer<ApplicationDbContext>(null);

The line above will link the AspNetIdentity entities to your tables without re-creating the tables.

Code Example:

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

    modelBuilder.Entity<ApplicationUserRole>().ToTable("UserRoles");
    modelBuilder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
    modelBuilder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
    modelBuilder.Entity<ApplicationRole>().ToTable("Roles");

    System.Data.Entity.Database.SetInitializer<ApplicationDbContext>(null);
}
Xavier Egea
  • 4,712
  • 3
  • 25
  • 39
  • Hi Freerider. The OnModelCreating method does not get called? I guess this is because I'm using a database first approach which is my requirement for this – Bob Oct 03 '14 at 00:25
  • Is your Database Context (ApplicationDbContext in my example) inheriting from IdentityDbContext? – Xavier Egea Oct 03 '14 at 07:48
  • 1
    Actually no, I'm inheriting from DbContext. Do I need to inherit from IdentityDbContext? I did inherit from it now and OnModelCreating still didn't get called. I think if I change the connecting string to a code-first string it will get called but I need to do this in a database-first way. And advice or a link to a useful example of this? – Bob Oct 03 '14 at 11:08
  • 1
    @Bob did you find a way to make it work with DatabaseFirst approach? – Shyamal Parikh Nov 20 '16 at 16:02