Add Product and related entity mappings
This commit is contained in:
parent
aefaac9140
commit
53df5dcdc1
1 changed files with 64 additions and 0 deletions
|
@ -90,5 +90,69 @@ public class ApplicationDbContext : DbContext
|
|||
entity.HasMany(ctv => ctv.Users)
|
||||
.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);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue