CodeLiturgy.Dashboard/BlueWest.Api/MySQL/ModelBuilderExtensions.cs

63 lines
1.7 KiB
C#

using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
namespace BlueWest.WebApi.MySQL
{
public static class ModelBuilderExtensions
{
public static void ProjectDatabaseModel(this ModelBuilder modelBuilder)
{
// Keys
CountryCurrencyModel(modelBuilder);
UserModel(modelBuilder);
}
private static void CountryCurrencyModel(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>(builder =>
{
builder.HasKey(x => x.Code);
});
modelBuilder.Entity<Currency>(builder =>
{
builder.HasKey(x => x.Num);
});
// Relationships
modelBuilder.Entity<Currency>()
.HasMany(ub => ub.Countries)
.WithMany(au => au.Currencies);
modelBuilder.Entity<Country>()
.HasMany(ub => ub.Currencies)
.WithMany(au => au.Countries);
}
private static void UserModel(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>(builder =>
builder
.HasMany<User>()
.WithOne(user => user.Country));
modelBuilder
.Entity<User>(builder => builder
.HasOne<Country>()
.WithMany(co => co.Users));
modelBuilder.Entity<FinanceOp>(builder =>
{
builder.HasOne<User>()
.WithMany(x => x.FinanceTransactions)
.HasForeignKey(x => x.UserId);
});
}
}
}