Timothy Schenk
1455bdd75a
All checks were successful
Test if Server can be built / build-server (push) Successful in 22s
41 lines
1.7 KiB
C#
41 lines
1.7 KiB
C#
namespace Server.DB;
|
|
|
|
using Documents;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
public class WonderkingContext : DbContext
|
|
{
|
|
private readonly IConfiguration configuration;
|
|
private readonly ILoggerFactory loggerFactory;
|
|
|
|
public WonderkingContext(ILoggerFactory loggerFactory, IConfiguration configuration)
|
|
{
|
|
this.loggerFactory = loggerFactory;
|
|
this.configuration = configuration;
|
|
}
|
|
|
|
public DbSet<Account> Accounts { get; set; }
|
|
public DbSet<Character> Characters { get; set; }
|
|
|
|
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);
|
|
|
|
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);
|
|
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)");
|
|
});
|
|
}
|