3

I'm using ASP .NET Core 2.1 for my project with IndividualAuthentication. I need extra properties for my User table so i inherited from IdentityUser like below :

 public class ApplicationUser : IdentityUser
{
    [Required]
    [DataType(DataType.Text)]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Text)]
    public string LastName { get; set; }
}

After this modification AspNetUsers table is not renamed. All other identity tables are renamed. I don't know why is this happening.

After creating ApplicationUser class i have replaced IdentityUser with ApplicationUser in my code of Startup.cs

Below is the code before Modification

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

After modification

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

This is my OnModelCreating method

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        base.OnModelCreating(builder);

        modelBuilder.Entity<ApplicationUser>().ToTable("User");
        //modelBuilder.Entity<IdentityUser>().ToTable("User");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<IdentityUserClaim<string>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserLogin<string>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityRoleClaim<string>().ToTable("RoleClaim");
        modelBuilder.Entity<IdentityUserToken<string>().ToTable("UserToken");
    }

Now i don't know what other thing i am missing for renaming AspNetUsers table. I didn't find any solution and still searching.

Zubair Rana
  • 2,006
  • 2
  • 15
  • 38
  • Most likely you are not using the correct base generic `IdentityDbContext`. Can we see it, e.g. `class ApplicationDbContext : IdentityDbContext<…>`? – Ivan Stoev Jul 21 '18 at 12:26
  • `public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } }` – Zubair Rana Jul 21 '18 at 12:39

1 Answers1

3

The fluent configuration is ok, but the identity class used as base for your context is not.

As explained in the Customizing the model (emphasis is mine):

The starting point for customizing the model is to derive from the appropriate context type; see the preceding section.

and the preceding sections explain the base classes, generic type arguments and the default configuration.

With that being said, since you are using only a custom IdentityUser derived class, the base at least should be the IdentityDbContext<TUser>:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    // ...
}
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • 1
    Thanks just found exactly same solution as you described and it solved my problem. Thanks again. – Zubair Rana Jul 21 '18 at 13:17
  • Can you please answer this question also? https://stackoverflow.com/questions/50785009/how-to-seed-an-admin-user-in-ef-core-2-1-0 – Zubair Rana Jul 21 '18 at 13:36
  • I wish I could. Unfortunately my experience is mainly with EF Core. All I can say is that from EF Core point of view the posted answer looks correct. But not sure if it fits in the identity infrastructure. – Ivan Stoev Jul 21 '18 at 14:16
  • just to add to the answer, I found microsoft docs very elaborate. https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-2.1#change-tablecolumn-names-and-facets – K.S Jan 10 '20 at 07:21