Files
public-valetax/Data/ApplicationDbContext.cs
2025-12-26 16:40:32 +03:00

56 lines
2.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using public_valetax.Models;
namespace public_valetax.Data
{
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : DbContext(options)
{
public DbSet<Tree> Trees { get; set; } = null!;
public DbSet<Node> Nodes { get; set; } = null!;
public DbSet<JournalEntry> JournalEntries { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Configure Tree entity
modelBuilder.Entity<Tree>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Name).IsRequired().HasMaxLength(255);
entity.HasIndex(e => e.Name).IsUnique();
});
// Configure Node entity
modelBuilder.Entity<Node>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Name).IsRequired().HasMaxLength(255);
// Self-referencing foreign key for parent-child relationship
entity.HasOne(n => n.Parent)
.WithMany(n => n.Children)
.HasForeignKey(n => n.ParentId)
.OnDelete(DeleteBehavior.Cascade);
// Foreign key to Tree
entity.HasOne(n => n.Tree)
.WithMany(t => t.Nodes)
.HasForeignKey(n => n.TreeId)
.OnDelete(DeleteBehavior.Cascade);
// Ensure unique node names within the same tree
entity.HasIndex(n => new { n.TreeId, n.Name }).IsUnique();
});
// Configure JournalEntry entity
modelBuilder.Entity<JournalEntry>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.ExceptionType).IsRequired().HasMaxLength(255);
entity.Property(e => e.Message).IsRequired();
entity.HasIndex(e => e.EventId).IsUnique();
});
}
}
}