Compare commits

..

2 commits

Author SHA1 Message Date
0a4c4abcbc Merge branch 'EntityFrameworkSetup' into EntityFrameworkSetupPropertyMap
# Conflicts:
#	DrinkRateAPI/Contexts/ApplicationDbContext.cs
2025-08-09 14:08:30 +02:00
53df5dcdc1 Add Product and related entity mappings 2025-08-09 14:05:37 +02:00

View file

@ -91,6 +91,70 @@ public class ApplicationDbContext : DbContext
.WithMany(u => u.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.UserId, pr.ProductId });
entity.HasOne(pr => pr.User)
.WithMany(u => u.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.Users)
.WithMany(u => u.ProductTableViews);
});
// User
modelBuilder.Entity<DbUser>(entity =>
{