Files
myAi/rag-api/Data/RagDbContext.cs
T
claude 20b4127fda
Build and Push Docker Images / build (push) Successful in 2s
Changes
2026-05-04 21:41:14 +03:00

73 lines
3.2 KiB
C#

using Api.Data.Entities;
using Microsoft.EntityFrameworkCore;
namespace Api.Data;
public sealed class RagDbContext : DbContext
{
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 OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RagDocumentEntity>(entity =>
{
entity.ToTable("RagDocuments");
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("RagChunks");
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("RagEmbeddingCache");
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("RagChatCompletionCache");
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()");
});
}
}