continuity/Server/DB/WonderkingContext.cs

42 lines
1.7 KiB
C#
Raw Normal View History

2023-10-12 07:15:34 +00:00
namespace Server.DB;
2023-08-10 08:47:35 +00:00
using Documents;
2023-08-14 11:49:27 +00:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
2023-08-10 08:47:35 +00:00
2023-08-14 11:49:27 +00:00
public class WonderkingContext : DbContext
2023-08-10 08:47:35 +00:00
{
private readonly IConfiguration _configuration;
private readonly ILoggerFactory _loggerFactory;
2023-08-14 11:49:27 +00:00
public WonderkingContext(ILoggerFactory loggerFactory, IConfiguration configuration)
2023-08-10 08:47:35 +00:00
{
this._loggerFactory = loggerFactory;
this._configuration = configuration;
2023-08-10 08:47:35 +00:00
}
2023-08-14 19:30:32 +00:00
public DbSet<Account> Accounts { get; set; }
2023-08-14 20:30:35 +00:00
public DbSet<Character> Characters { get; set; }
2023-08-14 19:30:32 +00:00
2023-08-14 11:49:27 +00:00
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder
.UseNpgsql(
$"Host={this._configuration["DB:Host"]};Username={this._configuration["DB:Username"]};Password={this._configuration["DB:Password"]};Database={this._configuration["DB:Database"]};Port={this._configuration["DB:Port"]}")
.EnableSensitiveDataLogging().UseLoggerFactory(this._loggerFactory);
2023-08-14 11:49:27 +00:00
protected override void OnModelCreating(ModelBuilder modelBuilder) =>
modelBuilder.Entity<Account>(builder =>
{
builder.Property(b => b.Username).HasColumnType("varchar(20)");
builder.Property(b => b.Password).HasColumnType("bytea");
builder.Property(b => b.Salt).HasColumnType("bytea");
builder.HasKey(b => b.Id);
2023-08-14 20:30:35 +00:00
builder.HasMany(e => e.Characters).WithOne(e => e.Account).HasForeignKey(e => e.AccountId).IsRequired();
}).Entity<Character>(builder =>
{
builder.HasKey(c => c.Id);
builder.Property(c => c.Name).HasColumnType("varchar(20)");
2023-08-14 11:49:27 +00:00
});
2023-08-11 09:31:30 +00:00
}