What I just did was to re-implement the functionality from IdentityDbContext straight on to my DbContext + the provided user stores. There is really no magic involved. The only thing the IdentityDbContext does is to add a few datasets and some mappings to your context.
You can find everything you need here: https://github.com/aspnet/AspNetIdentity/tree/master/src/Microsoft.AspNet.Identity.EntityFramework
We are using Guids as Ids to we already had custom classes like CustomUserLogin
which inherited from IdentityUserLogin
so I just moved everything from the base class into that class.
One thing you will see is that the old code doesn't have proper navigation properties in both directions. I added this so I could to the mapping like this:
private void SetupIdentityTables(ModelBuilder modelBuilder)
{
var user = modelBuilder.Entity<ApplicationUser>()
.ToTable("AspNetUsers");
user.HasMany(u => u.Roles).WithOne().HasForeignKey(ur => ur.UserId);
user.HasMany(u => u.Claims).WithOne().HasForeignKey(uc => uc.UserId);
user.HasMany(u => u.Logins).WithOne().HasForeignKey(ul => ul.UserId);
user.Property(u => u.UserName)
.IsRequired()
.HasMaxLength(256);
user.HasIndex(x => x.UserName).HasName("UserNameIndex").IsUnique();
// CONSIDER: u.Email is Required if set on options?
user.Property(u => u.Email).HasMaxLength(256);
modelBuilder.Entity<CustomUserRole>()
.ToTable("AspNetUserRoles")
.HasKey(r => new { r.UserId, r.RoleId });
modelBuilder.Entity<CustomUserLogin>()
.ToTable("AspNetUserLogins")
.HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId });
modelBuilder.Entity<CustomUserClaim>()
.ToTable("AspNetUserClaims");
var role = modelBuilder.Entity<CustomRole>()
.ToTable("AspNetRoles");
role.Property(r => r.Name)
.IsRequired()
.HasMaxLength(256);
role.HasIndex(x => x.Name).HasName("RoleNameIndex").IsUnique();
role.HasMany(r => r.Users).WithOne().HasForeignKey(ur => ur.RoleId);
}
NOTE: This code is actually not working perfectly as it has some foreign key problems. I will need to take another look at the mappings but you should get the idea at least.