2017-01-25 17:10:08 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
2016-11-22 18:40:47 +01:00
|
|
|
|
{
|
|
|
|
|
public class OrderingContext
|
2017-01-25 17:10:08 +01:00
|
|
|
|
: DbContext,IUnitOfWork
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
const string DEFAULT_SCHEMA = "ordering";
|
|
|
|
|
|
|
|
|
|
public DbSet<Order> Orders { get; set; }
|
|
|
|
|
|
|
|
|
|
public DbSet<OrderItem> OrderItems { get; set; }
|
|
|
|
|
|
|
|
|
|
public DbSet<Payment> Payments { get; set; }
|
|
|
|
|
|
|
|
|
|
public DbSet<Buyer> Buyers { get; set; }
|
|
|
|
|
|
2016-11-24 14:59:25 +01:00
|
|
|
|
public DbSet<CardType> CardTypes { get; set; }
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
|
|
|
|
public DbSet<OrderStatus> OrderStatus { get; set; }
|
|
|
|
|
|
|
|
|
|
public OrderingContext(DbContextOptions options) : base(options) { }
|
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
{
|
|
|
|
|
modelBuilder.Entity<Buyer>(ConfigureBuyer);
|
|
|
|
|
modelBuilder.Entity<Payment>(ConfigurePayment);
|
|
|
|
|
modelBuilder.Entity<Order>(ConfigureOrder);
|
|
|
|
|
modelBuilder.Entity<OrderItem>(ConfigureOrderItems);
|
2016-11-24 14:59:25 +01:00
|
|
|
|
modelBuilder.Entity<CardType>(ConfigureCardTypes);
|
|
|
|
|
modelBuilder.Entity<OrderStatus>(ConfigureOrderStatus);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureBuyer(EntityTypeBuilder<Buyer> buyerConfiguration)
|
|
|
|
|
{
|
|
|
|
|
buyerConfiguration.ToTable("buyers", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
buyerConfiguration.HasKey(b => b.Id);
|
|
|
|
|
|
|
|
|
|
buyerConfiguration.Property(b => b.Id)
|
|
|
|
|
.ForSqlServerUseSequenceHiLo("buyerseq", DEFAULT_SCHEMA);
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
buyerConfiguration.Property(b=>b.FullName)
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
2016-11-24 14:59:25 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
buyerConfiguration.HasIndex("FullName")
|
|
|
|
|
.IsUnique(true);
|
|
|
|
|
|
2016-11-24 14:59:25 +01:00
|
|
|
|
buyerConfiguration.HasMany(b => b.Payments)
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.WithOne()
|
|
|
|
|
.HasForeignKey("BuyerId")
|
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
|
|
var navigation = buyerConfiguration.Metadata.FindNavigation(nameof(Buyer.Payments));
|
|
|
|
|
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigurePayment(EntityTypeBuilder<Payment> paymentConfiguration)
|
|
|
|
|
{
|
|
|
|
|
paymentConfiguration.ToTable("payments", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
paymentConfiguration.HasKey(b => b.Id);
|
|
|
|
|
|
|
|
|
|
paymentConfiguration.Property(b => b.Id)
|
|
|
|
|
.ForSqlServerUseSequenceHiLo("paymentseq", DEFAULT_SCHEMA);
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
paymentConfiguration.Property<int>("BuyerId")
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
paymentConfiguration.Property<string>("CardHolderName")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
paymentConfiguration.Property<string>("Alias")
|
2017-01-18 16:51:44 -08:00
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
paymentConfiguration.Property<string>("CardNumber")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.HasMaxLength(25)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
paymentConfiguration.Property<DateTime>("Expiration")
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
paymentConfiguration.Property<int>("CardTypeId")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
paymentConfiguration.HasOne(p => p.CardType)
|
|
|
|
|
.WithMany()
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.HasForeignKey("CardTypeId");
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureOrder(EntityTypeBuilder<Order> orderConfiguration)
|
|
|
|
|
{
|
|
|
|
|
orderConfiguration.ToTable("orders", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
orderConfiguration.HasKey(o => o.Id);
|
|
|
|
|
|
|
|
|
|
orderConfiguration.Property(o => o.Id)
|
|
|
|
|
.ForSqlServerUseSequenceHiLo("orderseq", DEFAULT_SCHEMA);
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderConfiguration.Property<DateTime>("OrderDate").IsRequired();
|
|
|
|
|
orderConfiguration.Property<string>("Street").IsRequired();
|
|
|
|
|
orderConfiguration.Property<string>("State").IsRequired();
|
|
|
|
|
orderConfiguration.Property<string>("City").IsRequired();
|
|
|
|
|
orderConfiguration.Property<string>("ZipCode").IsRequired();
|
|
|
|
|
orderConfiguration.Property<int>("BuyerId").IsRequired();
|
|
|
|
|
orderConfiguration.Property<int>("OrderStatusId").IsRequired();
|
|
|
|
|
orderConfiguration.Property<int>("PaymentId").IsRequired();
|
|
|
|
|
|
|
|
|
|
var navigation = orderConfiguration.Metadata.FindNavigation(nameof(Order.OrderItems));
|
|
|
|
|
|
|
|
|
|
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
|
|
|
|
orderConfiguration.HasOne(o => o.Payment)
|
|
|
|
|
.WithMany()
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.HasForeignKey("PaymentId")
|
2016-11-24 14:59:25 +01:00
|
|
|
|
.OnDelete(DeleteBehavior.Restrict);
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
|
|
|
|
orderConfiguration.HasOne(o => o.Buyer)
|
|
|
|
|
.WithMany()
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.HasForeignKey("BuyerId");
|
2016-11-22 18:40:47 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderConfiguration.HasOne(o => o.OrderStatus)
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.WithMany()
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.HasForeignKey("OrderStatusId");
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureOrderItems(EntityTypeBuilder<OrderItem> orderItemConfiguration)
|
|
|
|
|
{
|
|
|
|
|
orderItemConfiguration.ToTable("orderItems", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
orderItemConfiguration.HasKey(o => o.Id);
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property(o => o.Id)
|
|
|
|
|
.ForSqlServerUseSequenceHiLo("orderitemseq");
|
|
|
|
|
|
|
|
|
|
orderItemConfiguration.Property<int>("OrderId")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property<decimal>("Discount")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property<int>("ProductId")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property<string>("ProductName")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
orderItemConfiguration.Property<decimal>("UnitPrice")
|
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
orderItemConfiguration.Property<int>("Units")
|
2016-11-22 18:40:47 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
}
|
2016-11-24 14:59:25 +01:00
|
|
|
|
|
|
|
|
|
void ConfigureOrderStatus(EntityTypeBuilder<OrderStatus> orderStatusConfiguration)
|
|
|
|
|
{
|
|
|
|
|
orderStatusConfiguration.ToTable("orderstatus", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
orderStatusConfiguration.HasKey(o => o.Id);
|
|
|
|
|
|
|
|
|
|
orderStatusConfiguration.Property(o => o.Id)
|
|
|
|
|
.HasDefaultValue(1)
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.ValueGeneratedNever()
|
2016-11-24 14:59:25 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
orderStatusConfiguration.Property(o => o.Name)
|
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureCardTypes(EntityTypeBuilder<CardType> cardTypesConfiguration)
|
|
|
|
|
{
|
|
|
|
|
cardTypesConfiguration.ToTable("cardtypes", DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
|
|
cardTypesConfiguration.HasKey(ct => ct.Id);
|
|
|
|
|
|
|
|
|
|
cardTypesConfiguration.Property(ct => ct.Id)
|
|
|
|
|
.HasDefaultValue(1)
|
2017-01-25 17:10:08 +01:00
|
|
|
|
.ValueGeneratedNever()
|
2016-11-24 14:59:25 +01:00
|
|
|
|
.IsRequired();
|
|
|
|
|
|
|
|
|
|
cardTypesConfiguration.Property(ct => ct.Name)
|
|
|
|
|
.HasMaxLength(200)
|
|
|
|
|
.IsRequired();
|
|
|
|
|
}
|
2016-11-22 18:40:47 +01:00
|
|
|
|
}
|
|
|
|
|
}
|