using FinlyticNews.Entities; using Microsoft.EntityFrameworkCore; namespace FinlyticNews.Database; /// /// Entity Framework Core database context for the news microservice, /// managing article sources, processed news, and matched assets. /// public class NewsDbContext : DbContext { /// /// Initializes a new instance of the class. /// /// The context configuration options. public NewsDbContext(DbContextOptions options) : base(options) { } /// /// Gets or sets the database set for configured article sources. /// public DbSet ArticleSources => Set(); /// /// Gets or sets the database set for news articles. /// public DbSet NewsArticles => Set(); /// /// Gets or sets the database set for matched assets. /// public DbSet MatchedAssets => Set(); /// /// Gets or sets the database set for microservice configuration settings. /// public DbSet Settings => Set(); /// /// Configures the model mapping, database constraints, and unique indices. /// /// The builder being used to construct the database schema model. protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // Configure unique index on SourceUrl for deduplication check (idempotency) modelBuilder.Entity() .HasIndex(a => a.SourceUrl) .IsUnique(); // Performance indexes for news queries modelBuilder.Entity() .HasIndex(a => new { a.PublishedAt, a.ScrapedAt }); modelBuilder.Entity() .HasIndex(a => a.Status); modelBuilder.Entity() .HasIndex(m => m.Isin); // Configure relationship between NewsArticle and MatchedAssets modelBuilder.Entity() .HasOne(m => m.NewsArticle) .WithMany(a => a.MatchedAssets) .HasForeignKey(m => m.NewsArticleId) .OnDelete(DeleteBehavior.Cascade); } }