157 lines
4.5 KiB
C#
157 lines
4.5 KiB
C#
using CodeLiturgy.Data.Application;
|
|
using CodeLiturgy.Data.Application.Users;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CodeLiturgy.Domain.Model
|
|
{
|
|
/// <summary>
|
|
/// Database model configuration extensions
|
|
/// </summary>
|
|
public static class ModelBuilderExtensions
|
|
{
|
|
#region Initialization
|
|
|
|
static ModelBuilderExtensions() { }
|
|
|
|
/// <summary>
|
|
/// Setup the database model
|
|
/// </summary>
|
|
/// <param name="modelBuilder"></param>
|
|
public static void ConfigureCurrentDbModel(this ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder
|
|
.ConfigureDatabaseKeys()
|
|
.EnvironmentModel()
|
|
.SiteModel()
|
|
.ConfigureUserModel()
|
|
.ConfigureAppContextModel();
|
|
|
|
//.ConfigureIdentityModel();
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region Application Users
|
|
|
|
/// <summary>
|
|
/// Configure App context model
|
|
/// </summary>
|
|
/// <param name="modelBuilder"></param>
|
|
private static void ConfigureAppContextModel(this ModelBuilder builder)
|
|
{
|
|
builder.Entity<ApplicationUserRole>().ToTable("UserRoles");
|
|
builder.Entity<ApplicationUser>(b =>
|
|
{
|
|
b.HasMany<ApplicationUserRole>()
|
|
.WithOne(b => b.User)
|
|
.HasForeignKey(ur => ur.UserId).IsRequired();
|
|
});
|
|
|
|
|
|
builder.Entity<ApplicationUser>().ToTable("ApplicationUser")
|
|
.HasKey(x => x.Id);
|
|
|
|
|
|
builder.Entity<ApplicationRole>(b =>
|
|
{
|
|
b.HasKey(r => r.Id);
|
|
b.HasIndex(r => r.NormalizedName).HasDatabaseName("RoleNameIndex").IsUnique();
|
|
b.ToTable("Roles");
|
|
b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();
|
|
|
|
b.Property(u => u.Name).HasMaxLength(256);
|
|
b.Property(u => u.NormalizedName).HasMaxLength(256);
|
|
|
|
b.HasMany<ApplicationUserRole>().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
|
|
b.HasMany<ApplicationRoleClaim>().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
|
|
});
|
|
|
|
builder.Entity<ApplicationUserRole>().HasOne(x => x.ApplicationRole);
|
|
builder.Entity<ApplicationRoleClaim>().HasOne<ApplicationRole>(x => x.ApplicationRole);
|
|
builder.Entity<ApplicationUserClaim>().HasOne<ApplicationUser>(x => x.ApplicationUser);
|
|
|
|
|
|
builder.Entity<ApplicationRoleClaim>(b =>
|
|
{
|
|
b.HasKey(rc => rc.Id);
|
|
b.ToTable("RoleClaims");
|
|
});
|
|
|
|
builder.Entity<ApplicationUserRole>(b =>
|
|
{
|
|
b.HasKey(r => new {r.UserId, r.RoleId});
|
|
b.ToTable("UserRoles");
|
|
});
|
|
|
|
builder.Entity<ApplicationRoleClaim>().ToTable("RoleClaims");
|
|
builder.Entity<ApplicationUserRole>().ToTable("UserRole");
|
|
|
|
|
|
|
|
//.ConfigureIdentityModel();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region DatabasePK
|
|
|
|
private static ModelBuilder ConfigureDatabaseKeys(this ModelBuilder modelBuilder)
|
|
{
|
|
|
|
// FinanceOp
|
|
return
|
|
modelBuilder;
|
|
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region Database Models
|
|
|
|
private static ModelBuilder EnvironmentModel(this ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder
|
|
.Entity<SiteEnvironment>()
|
|
.HasMany<Site>(b => b.Sites)
|
|
.WithOne(x => x.Environment)
|
|
.HasForeignKey(x => x.EnvironmentId);
|
|
|
|
return modelBuilder;
|
|
|
|
}
|
|
|
|
private static ModelBuilder SiteModel(this ModelBuilder modelBuilder)
|
|
{
|
|
|
|
modelBuilder.Entity<Site>()
|
|
.HasOne(x => x.Environment)
|
|
.WithMany(x => x.Sites)
|
|
.HasForeignKey(x => x.EnvironmentId);
|
|
return modelBuilder;
|
|
|
|
}
|
|
|
|
private static ModelBuilder ConfigureUserModel(this ModelBuilder modelBuilder)
|
|
{
|
|
|
|
modelBuilder
|
|
.Entity<ApplicationUser>()
|
|
.HasMany(x => x.Sites)
|
|
.WithOne(x => x.User);
|
|
|
|
modelBuilder
|
|
.Entity<ApplicationUser>()
|
|
.HasMany(x => x.Environments)
|
|
.WithOne(x => x.User);
|
|
|
|
return modelBuilder;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|
|
|