using Rag.Data.Entities; using Microsoft.EntityFrameworkCore; namespace Rag.Data; public sealed class RagDbContext : DbContext { public const string SchemaName = "rag"; public const string MigrationTableName = "_Migrations"; public RagDbContext(DbContextOptions options) : base(options) { } public DbSet RagDocuments => Set(); public DbSet RagChunks => Set(); public DbSet RagEmbeddingCache => Set(); public DbSet RagChatCompletionCache => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema(SchemaName); modelBuilder.Entity(entity => { entity.ToTable("Documents"); entity.HasKey(x => x.Id); entity.Property(x => x.Id).HasMaxLength(64); entity.Property(x => x.DocumentType).HasMaxLength(80).IsRequired(); entity.Property(x => x.Title).HasMaxLength(300).IsRequired(); entity.Property(x => x.SourceUrl).HasMaxLength(1200); entity.Property(x => x.RawText).IsRequired(); entity.Property(x => x.TextHash).HasMaxLength(64).IsRequired(); entity.Property(x => x.MetadataJson).HasDefaultValue("{}").IsRequired(); entity.Property(x => x.CreatedAt).HasDefaultValueSql("SYSUTCDATETIME()"); entity.HasIndex(x => x.TextHash); entity.HasIndex(x => x.DocumentType); }); modelBuilder.Entity(entity => { entity.ToTable("Chunks"); entity.HasKey(x => x.Id); entity.Property(x => x.Id).HasMaxLength(64); entity.Property(x => x.DocumentId).HasMaxLength(64).IsRequired(); entity.Property(x => x.Text).IsRequired(); entity.Property(x => x.Embedding).IsRequired(); entity.HasOne(x => x.Document) .WithMany(x => x.Chunks) .HasForeignKey(x => x.DocumentId) .OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity(entity => { entity.ToTable("EmbeddingCache"); entity.HasKey(x => x.CacheKey); entity.Property(x => x.CacheKey).HasMaxLength(64); entity.Property(x => x.Model).HasMaxLength(120).IsRequired(); entity.Property(x => x.TextHash).HasMaxLength(64).IsRequired(); entity.Property(x => x.Vector).IsRequired(); entity.Property(x => x.CreatedAt).HasDefaultValueSql("SYSUTCDATETIME()"); entity.HasIndex(x => x.TextHash); }); modelBuilder.Entity(entity => { entity.ToTable("ChatCompletionCache"); entity.HasKey(x => x.CacheKey); entity.Property(x => x.CacheKey).HasMaxLength(64); entity.Property(x => x.Model).HasMaxLength(120).IsRequired(); entity.Property(x => x.Temperature).HasColumnType("decimal(4,2)"); entity.Property(x => x.ResponseText).IsRequired(); entity.Property(x => x.CreatedAt).HasDefaultValueSql("SYSUTCDATETIME()"); }); } }