Files
claude 487924e345 Move RAG repository from rag-api to rag-data — consolidate data layer ownership
- Move IRagRepository, EfRagRepository, and VectorSerializer from rag-api/Data to rag-data/Repositories
- Add rag-api-models ProjectReference to rag-data.csproj for model type availability
- Delete rag-api/Data folder (no longer needed; all data access is now in rag-data)
- This aligns RAG with email-api and other services: all data code in the data project

Pattern: rag-api (API logic) → rag-data (repository, EF entities, migrations)

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-05-29 09:38:25 +03:00

85 lines
3.7 KiB
C#

using Rag.Data.Entities;
using Microsoft.EntityFrameworkCore;
namespace Rag.Data;
public sealed class RagDbContext : DbContext
{
public const string SchemaName = MigrationConstants.SchemaName;
public const string MigrationTableName = MigrationConstants.MigrationTableName;
public RagDbContext(DbContextOptions<RagDbContext> options) : base(options)
{
}
public DbSet<RagDocumentEntity> RagDocuments => Set<RagDocumentEntity>();
public DbSet<RagChunkEntity> RagChunks => Set<RagChunkEntity>();
public DbSet<RagEmbeddingCacheEntity> RagEmbeddingCache => Set<RagEmbeddingCacheEntity>();
public DbSet<RagChatCompletionCacheEntity> RagChatCompletionCache => Set<RagChatCompletionCacheEntity>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
// Configure migration history table to use schema-qualified name: [rag].[_Migrations]
optionsBuilder.UseSqlServer(x => x.MigrationsHistoryTable(MigrationTableName, SchemaName));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema(SchemaName);
modelBuilder.Entity<RagDocumentEntity>(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<RagChunkEntity>(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<RagEmbeddingCacheEntity>(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<RagChatCompletionCacheEntity>(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()");
});
}
}