using BlueWest.Data; using BlueWest.Data.Application; using BlueWest.WebApi.Context.Users; using Microsoft.EntityFrameworkCore; namespace BlueWest.Domain.Model { /// /// Database model configuration extensions /// public static class ModelBuilderExtensions { #region Initialization static ModelBuilderExtensions() { } /// /// Setup the database model /// /// public static void ConfigureCurrentDbModel(this ModelBuilder modelBuilder) { modelBuilder .ConfigureDatabaseKeys() .CurrencyModel() .ConfigureUserModel() .ConfigureAppContextModel(); //.ConfigureIdentityModel(); } #endregion #region Application Users /// /// Configure App context model /// /// private static void ConfigureAppContextModel(this ModelBuilder builder) { builder.Entity().ToTable("UserRoles"); builder.Entity(b => { b.HasMany() .WithOne(b => b.User) .HasForeignKey(ur => ur.UserId).IsRequired(); }); builder.Entity().ToTable("ApplicationUser") .HasKey(x => x.Id); builder.Entity(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().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired(); b.HasMany().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired(); }); builder.Entity().HasOne(x => x.ApplicationRole); builder.Entity().HasOne(x => x.ApplicationRole); builder.Entity().HasOne(x => x.ApplicationUser); builder.Entity(b => { b.HasKey(rc => rc.Id); b.ToTable("RoleClaims"); }); builder.Entity(b => { b.HasKey(r => new {r.UserId, r.RoleId}); b.ToTable("UserRoles"); }); builder.Entity().ToTable("RoleClaims"); builder.Entity().ToTable("UserRole"); // Session Token Primary Key builder.Entity(b => b.HasKey(x => x.Id)); builder.Entity(b => b.Property(x => x.Id).ValueGeneratedOnAdd()); // Session Data builder.Entity() .HasOne(b => b.User) .WithMany(x => x.SessionDatas) .HasForeignKey(x => x.UserId); // Session Data Primary Key builder.Entity(b => b.HasKey(x => x.Id)); builder.Entity(b => b.Property(x => x.Id).ValueGeneratedOnAdd()); //.ConfigureIdentityModel(); } #endregion #region DatabasePK private static ModelBuilder ConfigureDatabaseKeys(this ModelBuilder modelBuilder) { // User modelBuilder .Entity(builder => builder .HasKey(user => user.Id)) .Entity().Property(user => user.Id) .ValueGeneratedOnAdd(); // Country modelBuilder .Entity(builder => builder .HasKey(country => country.Id)) .Entity().Property(country => country.Id) .ValueGeneratedOnAdd(); // Currency modelBuilder .Entity(builder => builder .HasKey(currency => currency.Id)) .Entity() .Property(currency => currency.Id) .ValueGeneratedOnAdd(); // Company PK modelBuilder .Entity(builder => builder .HasKey(company => company.Id)) .Entity().Property(company => company.Id) .ValueGeneratedOnAdd(); // Industry PK modelBuilder .Entity(builder => builder .HasKey(industry => industry.Id)) .Entity().Property(industry => industry.Id) .ValueGeneratedOnAdd(); // Product modelBuilder .Entity(builder => builder .HasKey(industry => industry.Id)); modelBuilder .Entity() .Property(industry => industry.Id) .ValueGeneratedOnAdd(); // FinanceOp return modelBuilder.Entity(builder => builder .HasKey(financeOp => financeOp.Id)); } #endregion #region Database Models private static ModelBuilder CurrencyModel(this ModelBuilder modelBuilder) { modelBuilder.Entity().ToTable("Currencies"); modelBuilder.Entity().ToTable("Countries"); modelBuilder .Entity() .HasMany(ub => ub.Countries) .WithMany(au => au.Currencies); modelBuilder.Entity() .HasMany(ub => ub.Currencies) .WithMany(au => au.Countries); return modelBuilder; } private static ModelBuilder ConfigureUserModel(this ModelBuilder modelBuilder) { // FinanceOp => User modelBuilder .Entity(builder => builder .HasOne() .WithMany(ft => ft.FinanceTransactions) .HasForeignKey(x => x.UserId)); return modelBuilder; } #endregion } }