namespace SOR.Database.Contexts;
using Microsoft.EntityFrameworkCore;
using SOR.Database.Entities.Collection;
///
/// EF Core DbContext for the collection schema: player-owned cards and decks.
/// All cross-schema IDs (AccountId, CardDefinitionId) are bare Guids — no EF FKs.
///
/// Schema: "collection"
/// Run migrations: dotnet ef migrations add InitialCreate
/// --context CollectionDbContext
/// --project src/SOR.Database
/// --startup-project src/SOR.Admin
/// --output-dir Migrations/Collection
///
public sealed class CollectionDbContext : DbContext
{
public CollectionDbContext(DbContextOptions options)
: base(options) { }
public DbSet PlayerCards => Set();
public DbSet Decks => Set();
public DbSet DeckCards => Set();
protected override void OnModelCreating(ModelBuilder builder)
{
builder.HasDefaultSchema("collection");
builder.Entity(e =>
{
e.ToTable("player_cards");
e.HasKey(c => c.Id);
e.HasIndex(c => c.AccountId);
e.HasIndex(c => new { c.AccountId, c.CardDefinitionId });
});
builder.Entity(e =>
{
e.ToTable("decks");
e.HasKey(d => d.Id);
e.HasIndex(d => d.AccountId);
e.HasMany(d => d.Cards)
.WithOne(dc => dc.Deck)
.HasForeignKey(dc => dc.DeckId)
.OnDelete(DeleteBehavior.Cascade);
});
builder.Entity(e =>
{
e.ToTable("deck_cards");
e.HasKey(dc => dc.Id);
// Same card definition can only appear once per deck row (use Quantity for copies).
e.HasIndex(dc => new { dc.DeckId, dc.CardDefinitionId }).IsUnique();
});
}
}