using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate; namespace Microsoft.eShopWeb.Infrastructure.Data { public class CatalogContext : DbContext { public CatalogContext(DbContextOptions options) : base(options) { } public DbSet Baskets { get; set; } public DbSet CatalogItems { get; set; } public DbSet CatalogBrands { get; set; } public DbSet CatalogTypes { get; set; } public DbSet Orders { get; set; } public DbSet OrderItems { get; set; } public DbSet BasketItems { get; set; } protected override void OnModelCreating(ModelBuilder builder) { builder.Entity(ConfigureBasket); builder.Entity(ConfigureCatalogBrand); builder.Entity(ConfigureCatalogType); builder.Entity(ConfigureCatalogItem); builder.Entity(ConfigureOrder); builder.Entity(ConfigureOrderItem); builder.Entity
(ConfigureAddress); builder.Entity(ConfigurateCatalogItemOrdered); builder.Entity(ConfigureBasketItem); } private void ConfigureBasketItem(EntityTypeBuilder builder) { builder.Property(bi => bi.UnitPrice) .IsRequired(true) .HasColumnType("decimal(18,2)"); } private void ConfigurateCatalogItemOrdered(EntityTypeBuilder builder) { builder.Property(cio => cio.ProductName) .HasMaxLength(50) .IsRequired(); } private void ConfigureAddress(EntityTypeBuilder
builder) { builder.Property(a => a.ZipCode) .HasMaxLength(18) .IsRequired(); builder.Property(a => a.Street) .HasMaxLength(180) .IsRequired(); builder.Property(a => a.State) .HasMaxLength(60); builder.Property(a => a.Country) .HasMaxLength(90) .IsRequired(); builder.Property(a => a.City) .HasMaxLength(100) .IsRequired(); } private void ConfigureBasket(EntityTypeBuilder builder) { var navigation = builder.Metadata.FindNavigation(nameof(Basket.Items)); navigation.SetPropertyAccessMode(PropertyAccessMode.Field); } private void ConfigureCatalogItem(EntityTypeBuilder builder) { builder.ToTable("Catalog"); builder.Property(ci => ci.Id) .ForSqlServerUseSequenceHiLo("catalog_hilo") .IsRequired(); builder.Property(ci => ci.Name) .IsRequired(true) .HasMaxLength(50); builder.Property(ci => ci.Price) .IsRequired(true) .HasColumnType("decimal(18,2)"); builder.Property(ci => ci.PictureUri) .IsRequired(false); builder.HasOne(ci => ci.CatalogBrand) .WithMany() .HasForeignKey(ci => ci.CatalogBrandId); builder.HasOne(ci => ci.CatalogType) .WithMany() .HasForeignKey(ci => ci.CatalogTypeId); } private void ConfigureCatalogBrand(EntityTypeBuilder builder) { builder.ToTable("CatalogBrand"); builder.HasKey(ci => ci.Id); builder.Property(ci => ci.Id) .ForSqlServerUseSequenceHiLo("catalog_brand_hilo") .IsRequired(); builder.Property(cb => cb.Brand) .IsRequired() .HasMaxLength(100); } private void ConfigureCatalogType(EntityTypeBuilder builder) { builder.ToTable("CatalogType"); builder.HasKey(ci => ci.Id); builder.Property(ci => ci.Id) .ForSqlServerUseSequenceHiLo("catalog_type_hilo") .IsRequired(); builder.Property(cb => cb.Type) .IsRequired() .HasMaxLength(100); } private void ConfigureOrder(EntityTypeBuilder builder) { var navigation = builder.Metadata.FindNavigation(nameof(Order.OrderItems)); navigation.SetPropertyAccessMode(PropertyAccessMode.Field); builder.OwnsOne(o => o.ShipToAddress); } private void ConfigureOrderItem(EntityTypeBuilder builder) { builder.OwnsOne(i => i.ItemOrdered); builder.Property(oi => oi.UnitPrice) .IsRequired(true) .HasColumnType("decimal(18,2)"); } } }