@ -1,11 +1,15 @@
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
{
{
using System ;
using System.Threading.Tasks ;
using Domain.SeedWork ;
using EntityFrameworkCore.Metadata ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.EntityFrameworkCore.Metadata.Builders ;
using Microsoft.EntityFrameworkCore.Metadata.Builders ;
using Microsoft.eShopOnContainers.Services.Ordering.Domain ;
using Microsoft.eShopOnContainers.Services.Ordering.Domain ;
public class OrderingContext
public class OrderingContext
: DbContext
: DbContext , IUnitOfWork
{
{
const string DEFAULT_SCHEMA = "ordering" ;
const string DEFAULT_SCHEMA = "ordering" ;
@ -18,7 +22,7 @@
public DbSet < Buyer > Buyers { get ; set ; }
public DbSet < Buyer > Buyers { get ; set ; }
public DbSet < CardType > Cards { get ; set ; }
public DbSet < CardType > CardType s { get ; set ; }
public DbSet < OrderStatus > OrderStatus { get ; set ; }
public DbSet < OrderStatus > OrderStatus { get ; set ; }
@ -32,21 +36,23 @@
modelBuilder . Entity < Payment > ( ConfigurePayment ) ;
modelBuilder . Entity < Payment > ( ConfigurePayment ) ;
modelBuilder . Entity < Order > ( ConfigureOrder ) ;
modelBuilder . Entity < Order > ( ConfigureOrder ) ;
modelBuilder . Entity < OrderItem > ( ConfigureOrderItems ) ;
modelBuilder . Entity < OrderItem > ( ConfigureOrderItems ) ;
modelBuilder . Entity < OrderStatus > ( )
. ToTable ( "orderstatus" , DEFAULT_SCHEMA ) ;
modelBuilder . Entity < CardType > ( )
. ToTable ( "cardtypes" , DEFAULT_SCHEMA ) ;
modelBuilder . Entity < CardType > ( ConfigureCardTypes ) ;
modelBuilder . Entity < OrderStatus > ( ConfigureOrderStatus ) ;
modelBuilder . Entity < Address > ( ConfigureAddress ) ;
modelBuilder . Entity < Address > ( )
modelBuilder . Entity < Address > ( )
. ToTable ( "address" , DEFAULT_SCHEMA ) ;
. ToTable ( "address" , DEFAULT_SCHEMA ) ;
}
}
void ConfigureBuyer ( EntityTypeBuilder < Buyer > buyerConfiguration )
void ConfigureBuyer ( EntityTypeBuilder < Buyer > buyerConfiguration )
{
{
buyerConfiguration . ToTable ( "buyers" , DEFAULT_SCHEMA ) ;
buyerConfiguration . ToTable ( "buyers" , DEFAULT_SCHEMA ) ;
buyerConfiguration . HasIndex ( b = > b . FullName )
. IsUnique ( true ) ;
buyerConfiguration . HasKey ( b = > b . Id ) ;
buyerConfiguration . HasKey ( b = > b . Id ) ;
buyerConfiguration . Property ( b = > b . Id )
buyerConfiguration . Property ( b = > b . Id )
@ -55,6 +61,11 @@
buyerConfiguration . Property ( b = > b . FullName )
buyerConfiguration . Property ( b = > b . FullName )
. HasMaxLength ( 2 0 0 )
. HasMaxLength ( 2 0 0 )
. IsRequired ( ) ;
. IsRequired ( ) ;
buyerConfiguration . HasMany ( b = > b . Payments )
. WithOne ( )
. HasForeignKey ( p = > p . BuyerId )
. OnDelete ( DeleteBehavior . Cascade ) ;
}
}
void ConfigurePayment ( EntityTypeBuilder < Payment > paymentConfiguration )
void ConfigurePayment ( EntityTypeBuilder < Payment > paymentConfiguration )
@ -96,12 +107,8 @@
orderConfiguration . HasOne ( o = > o . Payment )
orderConfiguration . HasOne ( o = > o . Payment )
. WithMany ( )
. WithMany ( )
. HasForeignKey ( o = > o . PaymentId ) ;
orderConfiguration . HasOne ( o = > o . BillingAddress )
. WithMany ( )
. HasForeignKey ( o = > o . BillingAddressId )
. OnDelete ( EntityFrameworkCore . Metadata . DeleteBehavior . SetNull ) ;
. HasForeignKey ( o = > o . PaymentId )
. OnDelete ( DeleteBehavior . Restrict ) ;
orderConfiguration . HasOne ( o = > o . Buyer )
orderConfiguration . HasOne ( o = > o . Buyer )
. WithMany ( )
. WithMany ( )
@ -134,5 +141,40 @@
. ForSqlServerHasDefaultValue ( 1 )
. ForSqlServerHasDefaultValue ( 1 )
. IsRequired ( ) ;
. IsRequired ( ) ;
}
}
void ConfigureOrderStatus ( EntityTypeBuilder < OrderStatus > 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 ( 2 0 0 )
. IsRequired ( ) ;
}
void ConfigureCardTypes ( EntityTypeBuilder < CardType > 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 ( 2 0 0 )
. IsRequired ( ) ;
}
void ConfigureAddress ( EntityTypeBuilder < Address > addressConfiguration )
{
addressConfiguration . ToTable ( "address" , DEFAULT_SCHEMA ) ;
}
}
}
}
}