45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
|
using System;
|
||
|
using BlueWest.Data;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
using Microsoft.Extensions.Configuration;
|
||
|
|
||
|
namespace BlueWest.WebApi.MySQL;
|
||
|
|
||
|
public sealed class MysqlDbContext : DbContext
|
||
|
{
|
||
|
public DbSet<User> Users { get; set; }
|
||
|
public DbSet<FinanceTransaction> Transactions { get; set; }
|
||
|
public IConfiguration Configuration;
|
||
|
|
||
|
|
||
|
public MysqlDbContext(DbContextOptions<MysqlDbContext> options) : base(options)
|
||
|
{
|
||
|
Database.EnsureCreated();
|
||
|
|
||
|
}
|
||
|
|
||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||
|
{
|
||
|
/*optionsBuilder.UseMySql(
|
||
|
Configuration.GetConnectionString("LocalMySQL"),
|
||
|
new MySqlServerVersion(new Version(8, 0, 11))
|
||
|
);*/
|
||
|
}
|
||
|
|
||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||
|
{
|
||
|
base.OnModelCreating(modelBuilder);
|
||
|
modelBuilder.Entity<User>(builder =>
|
||
|
{
|
||
|
builder.HasKey(x => x.Id);
|
||
|
});
|
||
|
|
||
|
modelBuilder.Entity<FinanceTransaction>(builder =>
|
||
|
{
|
||
|
builder.HasOne<User>()
|
||
|
.WithMany(x => x.FinanceTransactions)
|
||
|
.HasForeignKey(x => x.UserId);
|
||
|
});
|
||
|
|
||
|
}
|
||
|
}
|