using BlueWest.Data; using Microsoft.EntityFrameworkCore; namespace BlueWest.WebApi.EF.Model { /// /// Database model configuration extensions /// public static class ModelBuilderExtensions { #region Initialization /// /// Setup the database model /// /// public static void ConfigureCurrentDbModel(this ModelBuilder modelBuilder) { modelBuilder .ConfigureDatabaseKeys() .CurrencyModel() .ConfigureUserModel(); } #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() .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) { // User => Country modelBuilder .Entity(builder => builder .HasOne() .WithMany(co => co.Users)); // Country => User modelBuilder .Entity(builder => builder .HasMany() .WithOne(u => u.Country) .HasForeignKey(x => x.CountryId)); // FinanceOp => User modelBuilder .Entity(builder => builder .HasOne() .WithMany(ft => ft.FinanceTransactions) .HasForeignKey(x => x.UserId)); return modelBuilder; } #endregion } }