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

111 lines
2.9 KiB
C#

using CodeLiturgy.Data;
using CodeLiturgy.Data.Application;
using CodeLiturgy.Data.Application.Users;
using CodeLiturgy.Data.Application;
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()
.ConfigureAppContextModel()
.ConfigureUserModel()
.ConfigureEnvironmentModel();
//.ConfigureIdentityModel();
}
#endregion
#region Application Users
/// <summary>
/// Configure App context model
/// </summary>
/// <param name="modelBuilder"></param>
private static ModelBuilder ConfigureAppContextModel(this ModelBuilder builder)
{
return builder;
//.ConfigureIdentityModel();
}
#endregion
#region DatabasePK
private static ModelBuilder ConfigureDatabaseKeys(this ModelBuilder modelBuilder)
{
// Site
modelBuilder
.Entity<Site>(builder => builder
.HasKey(site => site.Id))
.Entity<Site>().Property(site => site.Id)
.ValueGeneratedOnAdd();
// Site Environment
modelBuilder
.Entity<SiteEnvironment>(builder => builder
.HasKey(site => site.Id))
.Entity<Site>().Property(site => site.Id)
.ValueGeneratedOnAdd();
// FinanceOp
return
modelBuilder;
}
#endregion
#region Database Models
private static ModelBuilder ConfigureUserModel(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>()
.HasMany(x => x.Environments)
.WithOne(u => u.User)
.HasForeignKey(x => x.UserId);
return modelBuilder;
}
private static ModelBuilder ConfigureEnvironmentModel(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<SiteEnvironment>().ToTable("site_env");
modelBuilder
.Entity<SiteEnvironment>()
.HasMany(x => x.Sites)
.WithOne(x => x.Environment)
.HasForeignKey(x => x.EnvironmentId);
return modelBuilder;
}
#endregion
}
}