83 lines
3.1 KiB
C#
83 lines
3.1 KiB
C#
using FinlyticFundamentals.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace FinlyticFundamentals.Database;
|
|
|
|
public class FundamentalsDbContext : DbContext
|
|
{
|
|
public FundamentalsDbContext(DbContextOptions<FundamentalsDbContext> options) : base(options)
|
|
{
|
|
}
|
|
|
|
public DbSet<AssetFundamentalsEntity> AssetFundamentals => Set<AssetFundamentalsEntity>();
|
|
public DbSet<CompanyExecutiveEntity> CompanyExecutives => Set<CompanyExecutiveEntity>();
|
|
public DbSet<FinancialStatementEntity> FinancialStatements => Set<FinancialStatementEntity>();
|
|
public DbSet<ForwardEstimateEntity> ForwardEstimates => Set<ForwardEstimateEntity>();
|
|
public DbSet<TickerFundamentalsEntity> TickerFundamentals => Set<TickerFundamentalsEntity>();
|
|
public DbSet<FundamentalsSettingsEntity> Settings => Set<FundamentalsSettingsEntity>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
// AssetFundamentals Configurations
|
|
modelBuilder.Entity<AssetFundamentalsEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Isin);
|
|
entity.HasIndex(e => e.PrimaryTicker).IsUnique();
|
|
|
|
// Setup One-to-Many Relationships with cascades
|
|
entity.HasMany(e => e.Executives)
|
|
.WithOne(e => e.AssetFundamentals)
|
|
.HasForeignKey(e => e.Isin)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasMany(e => e.FinancialStatements)
|
|
.WithOne(e => e.AssetFundamentals)
|
|
.HasForeignKey(e => e.Isin)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasMany(e => e.Estimates)
|
|
.WithOne(e => e.AssetFundamentals)
|
|
.HasForeignKey(e => e.Isin)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasMany(e => e.TickerFundamentals)
|
|
.WithOne(e => e.AssetFundamentals)
|
|
.HasForeignKey(e => e.Isin)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
});
|
|
|
|
// CompanyExecutive Configurations
|
|
modelBuilder.Entity<CompanyExecutiveEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Id);
|
|
entity.HasIndex(e => e.Isin);
|
|
});
|
|
|
|
// FinancialStatement Configurations
|
|
modelBuilder.Entity<FinancialStatementEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Id);
|
|
entity.HasIndex(e => e.Isin);
|
|
// Compound Index to prevent duplicate statement entries
|
|
entity.HasIndex(e => new { e.Isin, e.PeriodType, e.EndDate }).IsUnique();
|
|
});
|
|
|
|
// ForwardEstimate Configurations
|
|
modelBuilder.Entity<ForwardEstimateEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Id);
|
|
entity.HasIndex(e => e.Isin);
|
|
entity.HasIndex(e => new { e.Isin, e.Period }).IsUnique();
|
|
});
|
|
|
|
// TickerFundamentals Configurations
|
|
modelBuilder.Entity<TickerFundamentalsEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Ticker);
|
|
entity.HasIndex(e => e.Isin);
|
|
});
|
|
}
|
|
}
|