I am using ASP.NET Core 2.2 Identity for my user management system.
I need to have several types of users ... for example, warehouse user and application user, which I create a base class that inherits from identity user class => IdentityUser<long>
public class BaseApplicationUser : IdentityUser<long>
{
public string FirstName { set; get; }
public string LastName { set; get; }
...
}
and warehouse user and store user inherit from BaseApplicationUser
to create different users.
public class ApplicationUser : BaseApplicationUser
{
}
I want to have just one table for all of them => AspNetUsers
on OnModelCreating
added this code :
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<ApplicationUser>();
builder.Entity<WarehouseApplicationUser>();
}
and in DbSets in Context :
public DbSet<ApplicationRole> ApplicationRole { get; set; }
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<WarehouseApplicationRole> WarehouseApplicationRole { get; set; }
public DbSet<WarehouseApplicationUser> WarehouseApplicationUsers { get; set; }
I need to create separate role classes for the users, like ApplicationRole
and WarehouseApplicationUsers
.
Whats is the problem?
ApplicationUser
, that field is successfully added to AspNetUsers
, but when I add a field to WarehouseApplicationUsers
, EF Core adds new tables named WarehouseApplicationUsers
and WarehouseApplicationRole
and WarehouseApplicationUserRole
and then adds a field to the WarehouseApplicationUsers
table... what is this!And finally, how can I inherit and build a hierarchy of different users? Thanks a lot
Problem 1:
How to create different users in asp.net core Identity ?
ASP.NET Core provides a built-in method:AddIdentityCore<TUser>
.
You can use it like this: services.AddIdentityCore<ApplicationUser>();
More details you can see this thread.
Problem 2:
Here is an example,you can compare to your code:
BaseApplicationUser:
public class BaseApplicationUser: IdentityUser
{
public string FirstName { set; get; }
public string LastName { set; get; }
}
ApplicationUser:
public class ApplicationUser : BaseApplicationUser
{
public string Year { get; set; }
}
WarehouseApplicationUser:
public class WarehouseApplicationUser : BaseApplicationUser
{
public string Age { get; set; }
}
ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<BaseApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<WarehouseApplicationUser> WarehouseApplicationUsers { get; set; }
}
Migration result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With