Define a one-to-one relationship between CompanyTable and ProductTable. Correct a typo in the CompanyTable DbSet name within the ApplicationDbContext to ensure correct database mapping and operations.
230 lines
No EOL
7.5 KiB
C#
230 lines
No EOL
7.5 KiB
C#
using DrinkRateAPI.DbEntities;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace DrinkRateAPI.Contexts;
|
|
|
|
public class ApplicationDbContext : IdentityDbContext<DbApplicationUser, IdentityRole<Guid>, Guid>
|
|
{
|
|
public DbSet<DbCompany> Companies { get; set; }
|
|
public DbSet<DbCompanyRating> CompanyRatings { get; set; }
|
|
public DbSet<DbCompanyTable> CompanyTable { get; set; }
|
|
public DbSet<DbCompanyTableView> CompanyTableViews { get; set; }
|
|
|
|
public DbSet<DbProduct> Product { get; set; }
|
|
public DbSet<DbProductRating> ProductRating { get; set; }
|
|
public DbSet<DbProductTable> ProductTable { get; set; }
|
|
public DbSet<DbProductTableView> ProductTableView { get; set; }
|
|
|
|
public DbSet<DbUserProfile> UserProfiles { get; set; }
|
|
public DbSet<DbUserProfileCompanyTableStat> UserProfileCompanyTableStats { get; set; }
|
|
public DbSet<DbUserProfileProductTableStat> UserProfileProductTableStats { get; set; }
|
|
|
|
public DbSet<DbUpdateRecord> UpdateRecords { get; set; }
|
|
public DbSet<DbUpdateRecordFieldChange> UpdateRecordFieldChanges { get; set; }
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
|
var configuration = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json")
|
|
.Build();
|
|
|
|
optionsBuilder.UseNpgsql(configuration.GetConnectionString($"Db{environment}"));
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
// Company
|
|
modelBuilder.Entity<DbCompany>(entity =>
|
|
{
|
|
entity.HasKey(c => c.Id);
|
|
|
|
entity.HasMany(c => c.Products)
|
|
.WithOne(p => p.Company)
|
|
.HasForeignKey(p => p.CompanyId);
|
|
|
|
entity.HasMany(c => c.CompanyRatings)
|
|
.WithOne(cr => cr.Company)
|
|
.HasForeignKey(cr => cr.CompanyId);
|
|
|
|
entity.Property(c => c.CompanyName)
|
|
.IsRequired()
|
|
.HasMaxLength(50);
|
|
|
|
entity.HasIndex(c => c.CompanyName)
|
|
.IsUnique();
|
|
|
|
// to do: index by Sum / Count
|
|
});
|
|
|
|
// Company Rating
|
|
modelBuilder.Entity<DbCompanyRating>(entity =>
|
|
{
|
|
entity.HasKey(cr => new { cr.UserProfileId, cr.CompanyId });
|
|
|
|
entity.HasOne(cr => cr.UserProfile)
|
|
.WithMany(up => up.CompanyRatings);
|
|
|
|
entity.HasOne(cr => cr.Company)
|
|
.WithMany(c => c.CompanyRatings);
|
|
});
|
|
|
|
// Company Table
|
|
modelBuilder.Entity<DbCompanyTable>(entity =>
|
|
{
|
|
entity.HasKey(ct => ct.Id);
|
|
|
|
entity.HasMany(ct => ct.Companies)
|
|
.WithOne(c => c.CompanyTable)
|
|
.HasForeignKey(c => c.CompanyTableId);
|
|
|
|
entity.HasIndex(ct => ct.CompanyTableName)
|
|
.IsUnique();
|
|
|
|
entity.HasOne(ct => ct.ProductTable)
|
|
.WithOne(pt => pt.CompanyTable)
|
|
.HasForeignKey<DbProductTable>(pt => pt.CompanyTableId);
|
|
});
|
|
|
|
// Company Table View
|
|
modelBuilder.Entity<DbCompanyTableView>(entity =>
|
|
{
|
|
entity.HasKey(ctv => ctv.Id);
|
|
|
|
entity.HasMany(ctv => ctv.CompanyTables)
|
|
.WithMany();
|
|
|
|
entity.HasMany(ctv => ctv.UserProfiles)
|
|
.WithMany(up => up.CompanyTableViews);
|
|
});
|
|
|
|
// Product
|
|
modelBuilder.Entity<DbProduct>(entity =>
|
|
{
|
|
entity.HasKey(p => p.Id);
|
|
|
|
entity.HasOne(p => p.ProductTable)
|
|
.WithMany(pt => pt.Products)
|
|
.HasForeignKey(p => p.ProductTableId);
|
|
|
|
entity.HasOne(p => p.Company)
|
|
.WithMany(c => c.Products)
|
|
.HasForeignKey(p => p.CompanyId);
|
|
|
|
entity.HasMany(p => p.ProductRatings)
|
|
.WithOne(pr => pr.Product)
|
|
.HasForeignKey(pr => pr.ProductId);
|
|
|
|
entity.Property(p => p.ProductName)
|
|
.IsRequired()
|
|
.HasMaxLength(50);
|
|
|
|
entity.HasIndex(c => c.ProductName)
|
|
.IsUnique();
|
|
|
|
// to do: index by Sum / Count
|
|
});
|
|
|
|
// Product Rating
|
|
modelBuilder.Entity<DbProductRating>(entity =>
|
|
{
|
|
entity.HasKey(pr => new { pr.UserProfileId, pr.ProductId });
|
|
|
|
entity.HasOne(pr => pr.UserProfile)
|
|
.WithMany(up => up.ProductRatings);
|
|
|
|
entity.HasOne(pr => pr.Product)
|
|
.WithMany(p => p.ProductRatings);
|
|
});
|
|
|
|
// Product Table
|
|
modelBuilder.Entity<DbProductTable>(entity =>
|
|
{
|
|
entity.HasKey(pt => pt.Id);
|
|
|
|
entity.HasMany(pt => pt.Products)
|
|
.WithOne(p => p.ProductTable)
|
|
.HasForeignKey(p => p.ProductTableId);
|
|
|
|
entity.HasIndex(pt => pt.ProductTableName)
|
|
.IsUnique();
|
|
});
|
|
|
|
// Product Table View
|
|
modelBuilder.Entity<DbProductTableView>(entity =>
|
|
{
|
|
entity.HasKey(ptv => ptv.Id);
|
|
|
|
entity.HasMany(ptv => ptv.ProductTables)
|
|
.WithMany();
|
|
|
|
entity.HasMany(ptv => ptv.UserProfiles)
|
|
.WithMany(u => u.ProductTableViews);
|
|
});
|
|
|
|
// User
|
|
modelBuilder.Entity<DbUserProfile>(entity =>
|
|
{
|
|
entity.HasKey(up => up.Id);
|
|
|
|
entity.HasMany(up => up.CompanyTableViews)
|
|
.WithMany(ctv => ctv.UserProfiles);
|
|
|
|
entity.HasMany(up => up.ProductTableViews)
|
|
.WithMany(ptv => ptv.UserProfiles);
|
|
|
|
entity.HasMany(up => up.CompanyRatings)
|
|
.WithOne(cr => cr.UserProfile)
|
|
.HasForeignKey(cr => cr.UserProfileId);
|
|
|
|
entity.HasMany(up => up.ProductRatings)
|
|
.WithOne(pr => pr.UserProfile)
|
|
.HasForeignKey(pr => pr.UserProfileId);
|
|
|
|
entity.HasMany(up => up.UserProfileCompanyTableStats)
|
|
.WithOne(upcts => upcts.UserProfile)
|
|
.HasForeignKey(upcts => upcts.UserProfileId);
|
|
|
|
entity.HasMany(up => up.UserProfileProductTableStats)
|
|
.WithOne(uppts => uppts.UserProfile)
|
|
.HasForeignKey(uppts => uppts.UserProfileId);
|
|
|
|
entity.HasIndex(up => up.UserName)
|
|
.IsUnique();
|
|
|
|
entity.HasOne(up => up.ApplicationUser)
|
|
.WithOne(au => au.UserProfile)
|
|
.HasForeignKey<DbUserProfile>(up => up.ApplicationUserId);
|
|
});
|
|
|
|
// User Company Table Stat
|
|
modelBuilder.Entity<DbUserProfileCompanyTableStat>(entity =>
|
|
{
|
|
entity.HasKey(upcts => new { UserId = upcts.UserProfileId, upcts.CompanyTableId });
|
|
|
|
entity.HasOne(upcts => upcts.UserProfile)
|
|
.WithMany(up => up.UserProfileCompanyTableStats);
|
|
|
|
entity.HasOne(upcts => upcts.CompanyTable)
|
|
.WithMany();
|
|
});
|
|
|
|
// User Product Table Stat
|
|
modelBuilder.Entity<DbUserProfileProductTableStat>(entity =>
|
|
{
|
|
entity.HasKey(uppts => new { UserId = uppts.UserProfileId, uppts.ProductTableId });
|
|
|
|
entity.HasOne(uppts => uppts.UserProfile)
|
|
.WithMany(up => up.UserProfileProductTableStats);
|
|
|
|
entity.HasOne(uppts => uppts.ProductTable)
|
|
.WithMany();
|
|
});
|
|
|
|
}
|
|
} |