CodeLiturgy.Dashboard/CodeLiturgy.Views/Models/ModelBuilderExtensions.cs

111 lines
2.9 KiB
C#
Raw Normal View History

2022-11-18 03:15:53 +03:00
using CodeLiturgy.Data;
using CodeLiturgy.Data.Application;
using CodeLiturgy.Data.Application.Users;
using CodeLiturgy.Data.Application;
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-18 03:15:53 +03:00
.ConfigureAppContextModel()
2022-09-17 22:13:35 +03:00
.ConfigureUserModel()
2022-11-18 03:15:53 +03:00
.ConfigureEnvironmentModel();
2022-09-17 22:13:35 +03:00
//.ConfigureIdentityModel();
}
#endregion
#region Application Users
/// <summary>
/// Configure App context model
/// </summary>
/// <param name="modelBuilder"></param>
2022-11-18 03:15:53 +03:00
private static ModelBuilder ConfigureAppContextModel(this ModelBuilder builder)
2022-09-17 22:13:35 +03:00
{
2022-11-18 03:15:53 +03:00
return builder;
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
// Site
2022-08-22 00:14:50 +03:00
modelBuilder
2022-11-18 03:15:53 +03:00
.Entity<Site>(builder => builder
.HasKey(site => site.Id))
.Entity<Site>().Property(site => site.Id)
2022-08-22 00:14:50 +03:00
.ValueGeneratedOnAdd();
2022-11-18 03:15:53 +03:00
// Site Environment
modelBuilder
.Entity<SiteEnvironment>(builder => builder
.HasKey(site => site.Id))
.Entity<Site>().Property(site => site.Id)
.ValueGeneratedOnAdd();
2022-08-22 00:14:50 +03:00
// FinanceOp
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-18 03:15:53 +03:00
private static ModelBuilder ConfigureUserModel(this ModelBuilder modelBuilder)
2022-08-20 05:47:32 +03:00
{
2022-11-18 03:15:53 +03:00
modelBuilder.Entity<ApplicationUser>()
.HasMany(x => x.Environments)
.WithOne(u => u.User)
.HasForeignKey(x => x.UserId);
2022-08-20 05:47:32 +03:00
return modelBuilder;
}
2022-11-18 03:15:53 +03:00
private static ModelBuilder ConfigureEnvironmentModel(this ModelBuilder modelBuilder)
2022-08-20 05:47:32 +03:00
{
2022-11-18 03:15:53 +03:00
modelBuilder.Entity<SiteEnvironment>().ToTable("site_env");
modelBuilder
.Entity<SiteEnvironment>()
.HasMany(x => x.Sites)
.WithOne(x => x.Environment)
.HasForeignKey(x => x.EnvironmentId);
2022-09-10 07:28:41 +03:00
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
}
}