How to change table name using First Entity Framework database

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

In the first approach of the code, you can override OnModelCreating and do something like this so that the user's information is stored in the MyUsers table and not the default AspNetUsers:

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

      

In my first database approach, I manually created the MyUsers table, but since OnModelCreating is not called, I don’t know how to set up the data to be saved in my new table?

+3


source to share


1 answer


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

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

      

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



Sample code:

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

      

0


source







All Articles