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

59 lines
1.4 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>
/// Table containing countries
/// </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
}
}