roadie/Roadie.Api.Library/Identity/ApplicationUserDbContext.cs
2018-12-26 13:39:13 -06:00

72 lines
No EOL
2.3 KiB
C#

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Roadie.Library.Identity
{
public class ApplicationUserDbContext : IdentityDbContext<
ApplicationUser, ApplicationRole, int,
ApplicationUserClaim, ApplicationUserRole, IdentityUserLogin<int>,
ApplicationRoleClaim, IdentityUserToken<int>>
{
public ApplicationUserDbContext(DbContextOptions<ApplicationUserDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(b =>
{
b.ToTable("user");
// Each User can have many UserClaims
b.HasMany(e => e.Claims)
.WithOne(e => e.User)
.HasForeignKey(uc => uc.UserId)
.IsRequired();
// Each User can have many entries in the UserRole join table
b.HasMany(e => e.UserRoles)
.WithOne(e => e.User)
.HasForeignKey(ur => ur.UserId)
.IsRequired();
});
builder.Entity<ApplicationRole>(b =>
{
b.ToTable("userrole");
b.HasKey(ar => ar.Id);
// Each Role can have many entries in the UserRole join table
b.HasMany(e => e.UserRoles)
.WithOne(e => e.Role)
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
// Each Role can have many associated RoleClaims
b.HasMany(e => e.RoleClaims)
.WithOne(e => e.Role)
.HasForeignKey(rc => rc.RoleId)
.IsRequired();
});
builder.Entity<ApplicationUserClaim>(b =>
{
b.ToTable("userClaims");
});
builder.Entity<ApplicationUserRole>(b =>
{
b.ToTable("usersInRoles");
});
builder.Entity<ApplicationRoleClaim>(b =>
{
b.ToTable("userRoleClaims");
});
}
}
}