94 lines
3.3 KiB
C#
94 lines
3.3 KiB
C#
using FinlyticCore.Entities.Settings;
|
|
using FinlyticFundamentals.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace FinlyticFundamentals.Database;
|
|
|
|
public class FundamentalsDbContext : DbContext
|
|
{
|
|
public FundamentalsDbContext(DbContextOptions<FundamentalsDbContext> options) : base(options)
|
|
{
|
|
}
|
|
|
|
public DbSet<SettingEntity> DynamicSettings => Set<SettingEntity>();
|
|
public DbSet<AssetDataEntity> AssetData => Set<AssetDataEntity>();
|
|
public DbSet<FundamentalDataEntity> FundamentalData => Set<FundamentalDataEntity>();
|
|
public DbSet<KeyExecutiveEntity> KeyExecutives => Set<KeyExecutiveEntity>();
|
|
public DbSet<AssetEventEntity> AssetEvents => Set<AssetEventEntity>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
modelBuilder.Entity<SettingEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Id);
|
|
entity.HasIndex(e => e.Key);
|
|
});
|
|
|
|
modelBuilder.Entity<AssetDataEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Isin);
|
|
|
|
entity.OwnsOne(e => e.PrimaryTicker, t =>
|
|
{
|
|
t.Property(p => p.Ticker).HasColumnName("PrimaryTicker").HasDefaultValue(string.Empty);
|
|
t.Property(p => p.Exchange).HasColumnName("PrimaryTickerExchange").HasDefaultValue(string.Empty);
|
|
});
|
|
|
|
entity.OwnsMany(e => e.AvailableTickers, t =>
|
|
{
|
|
t.ToTable("Tickers");
|
|
t.WithOwner().HasForeignKey("AssetDataIsin");
|
|
t.Property<Guid>("Id");
|
|
t.HasKey("Id");
|
|
t.Property(p => p.Ticker).HasColumnName("Ticker");
|
|
t.Property(p => p.Exchange).HasColumnName("Exchange");
|
|
t.HasIndex(p => p.Ticker);
|
|
});
|
|
|
|
entity.HasMany(e => e.FundamentalData)
|
|
.WithOne(e => e.AssetData)
|
|
.HasForeignKey(e => e.AssetDataIsin)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasMany(e => e.KeyExecutives)
|
|
.WithOne(e => e.AssetData)
|
|
.HasForeignKey(e => e.AssetDataIsin)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasMany(e => e.AssetEvents)
|
|
.WithOne(e => e.AssetData)
|
|
.HasForeignKey(e => e.AssetDataIsin)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
});
|
|
|
|
modelBuilder.Entity<FundamentalDataEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Isin);
|
|
|
|
entity.OwnsOne(e => e.Ticker, t =>
|
|
{
|
|
t.Property(p => p.Ticker).HasColumnName("Ticker").HasDefaultValue(string.Empty);
|
|
t.Property(p => p.Exchange).HasColumnName("TickerExchange").HasDefaultValue(string.Empty);
|
|
});
|
|
});
|
|
|
|
modelBuilder.Entity<KeyExecutiveEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Id);
|
|
});
|
|
|
|
modelBuilder.Entity<AssetEventEntity>(entity =>
|
|
{
|
|
entity.HasKey(e => e.Id);
|
|
|
|
entity.OwnsOne(e => e.Ticker, t =>
|
|
{
|
|
t.Property(p => p.Ticker).HasColumnName("Ticker").HasDefaultValue(string.Empty);
|
|
t.Property(p => p.Exchange).HasColumnName("TickerExchange").HasDefaultValue(string.Empty);
|
|
});
|
|
});
|
|
}
|
|
}
|