namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure { using System; using System.Threading.Tasks; using Domain.SeedWork; using EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.eShopOnContainers.Services.Ordering.Domain; public class OrderingContext : DbContext,IUnitOfWork { const string DEFAULT_SCHEMA = "ordering"; public DbSet Orders { get; set; } public DbSet OrderItems { get; set; } public DbSet Payments { get; set; } public DbSet Buyers { get; set; } public DbSet CardTypes { get; set; } public DbSet OrderStatus { get; set; } public DbSet
Addresses { get; set; } public OrderingContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(ConfigureBuyer); modelBuilder.Entity(ConfigurePayment); modelBuilder.Entity(ConfigureOrder); modelBuilder.Entity(ConfigureOrderItems); modelBuilder.Entity(ConfigureCardTypes); modelBuilder.Entity(ConfigureOrderStatus); modelBuilder.Entity
(ConfigureAddress); } void ConfigureBuyer(EntityTypeBuilder buyerConfiguration) { buyerConfiguration.ToTable("buyers", DEFAULT_SCHEMA); buyerConfiguration.HasIndex(b => b.FullName) .IsUnique(true); buyerConfiguration.HasKey(b => b.Id); buyerConfiguration.Property(b => b.Id) .ForSqlServerUseSequenceHiLo("buyerseq", DEFAULT_SCHEMA); buyerConfiguration.Property(b => b.FullName) .HasMaxLength(200) .IsRequired(); buyerConfiguration.HasMany(b => b.Payments) .WithOne() .HasForeignKey(p => p.BuyerId) .OnDelete(DeleteBehavior.Cascade); } void ConfigurePayment(EntityTypeBuilder paymentConfiguration) { paymentConfiguration.ToTable("payments", DEFAULT_SCHEMA); paymentConfiguration.HasKey(b => b.Id); paymentConfiguration.Property(b => b.Id) .ForSqlServerUseSequenceHiLo("paymentseq", DEFAULT_SCHEMA); paymentConfiguration.Property(p => p.CardHolderName) .HasMaxLength(200) .IsRequired(); paymentConfiguration.Property(p => p.CardNumber) .HasMaxLength(25) .IsRequired(); paymentConfiguration.Property(p => p.Expiration) .IsRequired(); paymentConfiguration.HasOne(p => p.CardType) .WithMany() .HasForeignKey(p => p.CardTypeId); } void ConfigureOrder(EntityTypeBuilder orderConfiguration) { orderConfiguration.ToTable("orders", DEFAULT_SCHEMA); orderConfiguration.HasKey(o => o.Id); orderConfiguration.Property(o => o.Id) .ForSqlServerUseSequenceHiLo("orderseq", DEFAULT_SCHEMA); orderConfiguration.Property(o => o.OrderDate) .IsRequired(); orderConfiguration.HasOne(o => o.Payment) .WithMany() .HasForeignKey(o => o.PaymentId) .OnDelete(DeleteBehavior.Restrict); orderConfiguration.HasOne(o => o.Buyer) .WithMany() .HasForeignKey(o => o.BuyerId); orderConfiguration.HasOne(o => o.Status) .WithMany() .HasForeignKey(o => o.StatusId); } void ConfigureOrderItems(EntityTypeBuilder orderItemConfiguration) { orderItemConfiguration.ToTable("orderItems", DEFAULT_SCHEMA); orderItemConfiguration.HasKey(o => o.Id); orderItemConfiguration.Property(o => o.Discount) .IsRequired(); orderItemConfiguration.Property(o => o.ProductId) .IsRequired(); orderItemConfiguration.Property(o => o.ProductName) .IsRequired(); orderItemConfiguration.Property(o => o.UnitPrice) .IsRequired(); orderItemConfiguration.Property(o => o.Units) .ForSqlServerHasDefaultValue(1) .IsRequired(); } void ConfigureOrderStatus(EntityTypeBuilder orderStatusConfiguration) { orderStatusConfiguration.ToTable("orderstatus", DEFAULT_SCHEMA); orderStatusConfiguration.HasKey(o => o.Id); orderStatusConfiguration.Property(o => o.Id) .HasDefaultValue(1) .IsRequired(); orderStatusConfiguration.Property(o => o.Name) .HasMaxLength(200) .IsRequired(); } void ConfigureCardTypes(EntityTypeBuilder cardTypesConfiguration) { cardTypesConfiguration.ToTable("cardtypes", DEFAULT_SCHEMA); cardTypesConfiguration.HasKey(ct => ct.Id); cardTypesConfiguration.Property(ct => ct.Id) .HasDefaultValue(1) .IsRequired(); cardTypesConfiguration.Property(ct => ct.Name) .HasMaxLength(200) .IsRequired(); } void ConfigureAddress(EntityTypeBuilder
addressConfiguration) { addressConfiguration.ToTable("address", DEFAULT_SCHEMA); } } }