using DrinkRateAPI.DbEntities; using Microsoft.EntityFrameworkCore; namespace DrinkRateAPI.Contexts; public class ApplicationDbContext : DbContext { public DbSet Companies { get; set; } public DbSet CompanyRatings { get; set; } public DbSet CompanyTables { get; set; } public DbSet CompanyTableViews { get; set; } public DbSet Product { get; set; } public DbSet ProductRating { get; set; } public DbSet ProductTable { get; set; } public DbSet ProductTableView { get; set; } public DbSet Users { get; set; } public DbSet UserCompanyTableStats { get; set; } public DbSet UserProductTableStats { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); optionsBuilder.UseNpgsql(configuration.GetConnectionString("Local")); } protected override void OnModelCreating(ModelBuilder modelBuilder) { // Company modelBuilder.Entity(c => { c.HasKey(c => c.Id); c.HasMany(c => c.Products) .WithOne(p => p.Company) .HasForeignKey(p => p.CompanyId); c.HasMany(c => c.CompanyRatings) .WithOne(cr => cr.Company) .HasForeignKey(cr => cr.CompanyId); c.Property(c => c.CompanyName) .IsRequired() .HasMaxLength(50); c.HasIndex(c => c.CompanyName) .IsUnique(); // to do: index by Sum / Count }); // Company Rating modelBuilder.Entity(cr => { cr.HasKey(cr => new { cr.UserId, cr.CompanyId }); cr.HasOne(cr => cr.User) .WithMany(u => u.CompanyRatings); cr.HasOne(cr => cr.Company) .WithMany(c => c.CompanyRatings); }); // to do: othe objects } }