public class DatabaseContext(DbContextOptions<DatabaseContext> options, IConfiguration config) : IdentityDbContext<ApplicationUser>(options)
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql(config["Database:ConnectionString"]);
public DbSet<Block> Blocks { get; set; }
public DbSet<Palette> Palettes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Block>()
.HasIndex(b => b.MinecraftId)
.IsUnique();
modelBuilder.Entity<Palette>()
.HasMany(p => p.Blocks)
.WithMany(b => b.Palettes)
.UsingEntity(j => j.ToTable("PaletteBlocks"));
modelBuilder.Entity<Palette>()
.HasOne(p => p.PlaceholderBlock)
.WithMany()
.HasForeignKey(p => p.PlaceholderBlockId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.Palettes)
.WithOne(p => p.User)
.HasForeignKey(p => p.UserId);
modelBuilder.Entity<IdentityUserLogin<string>>(entity =>
{
entity.HasKey(l => new { l.LoginProvider, l.ProviderKey });
});
modelBuilder.Entity<IdentityUserRole<string>>(entity =>
{
entity.HasKey(r => new { r.UserId, r.RoleId });
});
modelBuilder.Entity<IdentityUserToken<string>>(entity =>
{
entity.HasKey(t => new { t.UserId, t.LoginProvider, t.Name });
});
}
}