2022-11-30 18:52:55 +03:00
|
|
|
using CodeLiturgy.Data.Application;
|
2022-11-18 03:15:53 +03:00
|
|
|
using CodeLiturgy.Data.Application.Users;
|
2022-08-20 05:47:32 +03:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
2022-10-30 19:48:24 +03:00
|
|
|
namespace CodeLiturgy.Domain.Model
|
2022-08-20 05:47:32 +03:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Database model configuration extensions
|
|
|
|
/// </summary>
|
|
|
|
public static class ModelBuilderExtensions
|
|
|
|
{
|
2022-08-22 00:14:50 +03:00
|
|
|
#region Initialization
|
2022-08-22 02:51:45 +03:00
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
static ModelBuilderExtensions() { }
|
2022-08-22 02:51:45 +03:00
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Setup the database model
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="modelBuilder"></param>
|
|
|
|
public static void ConfigureCurrentDbModel(this ModelBuilder modelBuilder)
|
|
|
|
{
|
|
|
|
modelBuilder
|
|
|
|
.ConfigureDatabaseKeys()
|
2022-11-30 18:52:55 +03:00
|
|
|
.EnvironmentModel()
|
|
|
|
.SiteModel()
|
2022-09-17 22:13:35 +03:00
|
|
|
.ConfigureUserModel()
|
2022-11-18 03:48:52 +03:00
|
|
|
.ConfigureAppContextModel();
|
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
//.ConfigureIdentityModel();
|
|
|
|
}
|
|
|
|
|
2022-12-09 03:27:00 +03:00
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Application Users
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Configure App context model
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="modelBuilder"></param>
|
2022-11-18 03:48:52 +03:00
|
|
|
private static void ConfigureAppContextModel(this ModelBuilder builder)
|
2022-09-17 22:13:35 +03:00
|
|
|
{
|
2022-11-18 03:48:52 +03:00
|
|
|
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");
|
|
|
|
|
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
//.ConfigureIdentityModel();
|
2022-08-20 05:47:32 +03:00
|
|
|
}
|
2022-08-22 00:14:50 +03:00
|
|
|
|
|
|
|
#endregion
|
2022-08-20 05:47:32 +03:00
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
#region DatabasePK
|
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
private static ModelBuilder ConfigureDatabaseKeys(this ModelBuilder modelBuilder)
|
|
|
|
{
|
2022-11-18 03:15:53 +03:00
|
|
|
|
2022-12-02 05:31:11 +03:00
|
|
|
modelBuilder.Entity<ApplicationUser>(x => x.HasKey(x => x.Id));
|
|
|
|
modelBuilder.Entity<ApplicationUser>().Property(x => x.Id).ValueGeneratedOnAdd();
|
|
|
|
|
|
|
|
modelBuilder.Entity<Site>(x => x.HasKey(x => x.Id));
|
|
|
|
modelBuilder.Entity<Site>().Property(x => x.Id).ValueGeneratedOnAdd();
|
|
|
|
|
|
|
|
modelBuilder.Entity<SiteEnvironment>(x => x.HasKey(x => x.Id));
|
|
|
|
modelBuilder.Entity<SiteEnvironment>().Property(x => x.Id).ValueGeneratedOnAdd();
|
|
|
|
|
|
|
|
|
2022-10-30 19:40:33 +03:00
|
|
|
return
|
|
|
|
modelBuilder;
|
|
|
|
|
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
}
|
2022-08-22 00:14:50 +03:00
|
|
|
#endregion
|
2022-08-20 05:47:32 +03:00
|
|
|
|
2022-08-22 00:14:50 +03:00
|
|
|
#region Database Models
|
|
|
|
|
2022-11-30 18:52:55 +03:00
|
|
|
private static ModelBuilder EnvironmentModel(this ModelBuilder modelBuilder)
|
2022-08-20 05:47:32 +03:00
|
|
|
{
|
2022-11-30 18:52:55 +03:00
|
|
|
modelBuilder
|
|
|
|
.Entity<SiteEnvironment>()
|
|
|
|
.HasMany<Site>(b => b.Sites)
|
|
|
|
.WithOne(x => x.Environment)
|
|
|
|
.HasForeignKey(x => x.EnvironmentId);
|
|
|
|
|
|
|
|
return modelBuilder;
|
2022-08-20 05:47:32 +03:00
|
|
|
|
2022-11-30 18:52:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static ModelBuilder SiteModel(this ModelBuilder modelBuilder)
|
|
|
|
{
|
|
|
|
|
|
|
|
modelBuilder.Entity<Site>()
|
|
|
|
.HasOne(x => x.Environment)
|
|
|
|
.WithMany(x => x.Sites)
|
|
|
|
.HasForeignKey(x => x.EnvironmentId);
|
2022-08-20 05:47:32 +03:00
|
|
|
return modelBuilder;
|
2022-11-18 03:48:52 +03:00
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
}
|
2022-11-18 03:48:52 +03:00
|
|
|
|
|
|
|
private static ModelBuilder ConfigureUserModel(this ModelBuilder modelBuilder)
|
2022-08-20 05:47:32 +03:00
|
|
|
{
|
2022-09-10 07:28:41 +03:00
|
|
|
|
2022-11-30 18:52:55 +03:00
|
|
|
modelBuilder
|
|
|
|
.Entity<ApplicationUser>()
|
|
|
|
.HasMany(x => x.Sites)
|
|
|
|
.WithOne(x => x.User);
|
|
|
|
|
|
|
|
modelBuilder
|
|
|
|
.Entity<ApplicationUser>()
|
|
|
|
.HasMany(x => x.Environments)
|
|
|
|
.WithOne(x => x.User);
|
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
return modelBuilder;
|
|
|
|
}
|
2022-08-22 00:14:50 +03:00
|
|
|
|
2022-09-06 07:54:48 +03:00
|
|
|
#endregion
|
2022-09-17 22:13:35 +03:00
|
|
|
|
2022-09-06 07:54:48 +03:00
|
|
|
|
2022-08-20 05:47:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|