roadie/Roadie.Api.Library/Identity/ApplicationUserDbContext.cs

72 lines
2.3 KiB
C#
Raw Normal View History

2018-11-10 23:26:04 +00:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2018-11-02 21:04:49 +00:00
using Microsoft.EntityFrameworkCore;
namespace Roadie.Library.Identity
{
2018-11-10 23:26:04 +00:00
public class ApplicationUserDbContext : IdentityDbContext<
ApplicationUser, ApplicationRole, int,
ApplicationUserClaim, ApplicationUserRole, IdentityUserLogin<int>,
ApplicationRoleClaim, IdentityUserToken<int>>
2018-11-02 21:04:49 +00:00
{
2018-11-10 23:26:04 +00:00
public ApplicationUserDbContext(DbContextOptions<ApplicationUserDbContext> options)
: base(options)
2018-11-02 21:04:49 +00:00
{
}
2018-11-10 23:26:04 +00:00
protected override void OnModelCreating(ModelBuilder builder)
2018-11-02 21:04:49 +00:00
{
2018-11-10 23:26:04 +00:00
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");
});
2018-11-02 21:04:49 +00:00
2018-11-10 23:26:04 +00:00
builder.Entity<ApplicationRoleClaim>(b =>
{
b.ToTable("userRoleClaims");
});
2018-11-02 21:04:49 +00:00
}
}
2018-11-04 20:33:37 +00:00
}