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

66 lines
1.7 KiB
C#
Raw Normal View History

2022-08-13 06:35:36 +03:00
using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace BlueWest.WebApi.MySQL;
2022-08-13 20:15:43 +03:00
/// <summary>
2022-08-19 02:02:57 +03:00
/// Countries and Currencies
2022-08-13 20:15:43 +03:00
/// </summary>
2022-08-13 06:35:36 +03:00
public class CountriesDbContext : DbContext
{
public DbSet<Country> Countries { get; set; }
public DbSet<Currency> Currencies { get; set; }
public IConfiguration Configuration;
2022-08-13 20:15:43 +03:00
/// <summary>
/// Options to be injected in the app
/// </summary>
/// <param name="options"></param>
public CountriesDbContext(DbContextOptions<CountriesDbContext> options) : base(options)
2022-08-13 06:35:36 +03:00
{
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
/*optionsBuilder.UseMySql(
Configuration.GetConnectionString("LocalMySQL"),
new MySqlServerVersion(new Version(8, 0, 11))
);*/
}
2022-08-13 20:15:43 +03:00
/// <summary>
/// The country number is the primary key
/// The currency code is the primary key
/// </summary>
/// <param name="modelBuilder"></param>
2022-08-13 06:35:36 +03:00
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
2022-08-13 20:15:43 +03:00
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Country>(builder =>
{
builder.HasKey(x => x.Code);
});
modelBuilder.Entity<Currency>(builder =>
{
builder.HasKey(x => x.Num);
});
2022-08-13 06:35:36 +03:00
2022-08-17 23:21:00 +03:00
modelBuilder.Entity<Currency>()
.HasMany(ub => ub.Countries)
.WithMany(au => au.Currencies);
modelBuilder.Entity<Country>()
.HasMany(ub => ub.Currencies)
.WithMany(au => au.Countries);
2022-08-13 06:35:36 +03:00
}
}