using BlueWest.Data; using Microsoft.EntityFrameworkCore; namespace BlueWest.WebApi.MySQL.Model { /// /// Database model configuration extensions /// public static class ModelBuilderExtensions { /// /// Setup the database model /// /// public static void ConfigureCurrentDbModel(this ModelBuilder modelBuilder) { modelBuilder .ConfigureDatabaseKeys() .CountryCurrencyModel() .ConfigureUserModel(); } private static ModelBuilder ConfigureDatabaseKeys(this ModelBuilder modelBuilder) { // User PK modelBuilder.Entity(builder => builder .HasKey(user => user.Id)); // Country PK modelBuilder.Entity(builder => builder .HasKey(country => country.Code)); // Currency PK modelBuilder.Entity(builder => builder .HasKey(currency => currency.Num)); // FinanceOp PK return modelBuilder.Entity(builder => builder .HasKey(financeOp => financeOp.Id)); } private static ModelBuilder CountryCurrencyModel(this ModelBuilder modelBuilder) { // Relationships 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; } } }