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

66 lines
1.7 KiB
C#

using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace BlueWest.WebApi.MySQL;
/// <summary>
/// Table containing countries
/// </summary>
public class CountriesDbContext : DbContext
{
public DbSet<Country> Countries { get; set; }
public DbSet<Currency> Currencies { get; set; }
public IConfiguration Configuration;
/// <summary>
/// Options to be injected in the app
/// </summary>
/// <param name="options"></param>
public CountriesDbContext(DbContextOptions<CountriesDbContext> options) : base(options)
{
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
/*optionsBuilder.UseMySql(
Configuration.GetConnectionString("LocalMySQL"),
new MySqlServerVersion(new Version(8, 0, 11))
);*/
}
/// <summary>
/// The country number is the primary key
/// The currency code is the primary key
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Country>(builder =>
{
builder.HasKey(x => x.Code);
});
modelBuilder.Entity<Currency>(builder =>
{
builder.HasKey(x => x.Num);
});
modelBuilder.Entity<Currency>()
.HasMany(ub => ub.Countries)
.WithMany(au => au.Currencies);
modelBuilder.Entity<Country>()
.HasMany(ub => ub.Currencies)
.WithMany(au => au.Countries);
}
}