Added IEntityTypeConfiguration feature from EF Core 2
This commit is contained in:
parent
4a6fc5299a
commit
70e429b760
@ -12,6 +12,9 @@
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<Content Update="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Update="wwwroot;">
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
|
@ -1,9 +1,8 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure
|
||||
{
|
||||
using EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using EntityConfigurations;
|
||||
using Model;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
|
||||
|
||||
public class CatalogContext : DbContext
|
||||
{
|
||||
@ -16,68 +15,9 @@
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
builder.Entity<CatalogBrand>(ConfigureCatalogBrand);
|
||||
builder.Entity<CatalogType>(ConfigureCatalogType);
|
||||
builder.Entity<CatalogItem>(ConfigureCatalogItem);
|
||||
builder.ApplyConfiguration(new CatalogBrandEntityTypeConfiguration());
|
||||
builder.ApplyConfiguration(new CatalogTypeEntityTypeConfiguration());
|
||||
builder.ApplyConfiguration(new CatalogItemEntityTypeConfiguration());
|
||||
}
|
||||
|
||||
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> 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);
|
||||
|
||||
builder.Property(ci => ci.PictureFileName)
|
||||
.IsRequired(false);
|
||||
|
||||
builder.Ignore(ci => ci.PictureUri);
|
||||
|
||||
builder.HasOne(ci => ci.CatalogBrand)
|
||||
.WithMany()
|
||||
.HasForeignKey(ci => ci.CatalogBrandId);
|
||||
|
||||
builder.HasOne(ci => ci.CatalogType)
|
||||
.WithMany()
|
||||
.HasForeignKey(ci => ci.CatalogTypeId);
|
||||
}
|
||||
|
||||
void ConfigureCatalogBrand(EntityTypeBuilder<CatalogBrand> 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);
|
||||
}
|
||||
|
||||
void ConfigureCatalogType(EntityTypeBuilder<CatalogType> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class CatalogBrandEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<CatalogBrand>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<CatalogBrand> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class CatalogItemEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<CatalogItem>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<CatalogItem> 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);
|
||||
|
||||
builder.Property(ci => ci.PictureFileName)
|
||||
.IsRequired(false);
|
||||
|
||||
builder.Ignore(ci => ci.PictureUri);
|
||||
|
||||
builder.HasOne(ci => ci.CatalogBrand)
|
||||
.WithMany()
|
||||
.HasForeignKey(ci => ci.CatalogBrandId);
|
||||
|
||||
builder.HasOne(ci => ci.CatalogType)
|
||||
.WithMany()
|
||||
.HasForeignKey(ci => ci.CatalogTypeId);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class CatalogTypeEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<CatalogType>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<CatalogType> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class CampaignEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<Campaign>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Campaign> builder)
|
||||
{
|
||||
builder.ToTable("Campaign");
|
||||
|
||||
builder.HasKey(m => m.Id);
|
||||
|
||||
builder.Property(m => m.Id)
|
||||
.ForSqlServerUseSequenceHiLo("campaign_hilo")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.Name)
|
||||
.HasColumnName("Name")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.From)
|
||||
.HasColumnName("From")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.To)
|
||||
.HasColumnName("To")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.Description)
|
||||
.HasColumnName("Description")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.PictureUri)
|
||||
.HasColumnName("PictureUri")
|
||||
.IsRequired();
|
||||
|
||||
builder.HasMany(m => m.Rules)
|
||||
.WithOne(r => r.Campaign)
|
||||
.HasForeignKey(r => r.CampaignId)
|
||||
.IsRequired();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class RuleEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<Rule>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Rule> builder)
|
||||
{
|
||||
builder.ToTable("Rule");
|
||||
|
||||
builder.HasKey(r => r.Id);
|
||||
|
||||
builder.Property(r => r.Id)
|
||||
.ForSqlServerUseSequenceHiLo("rule_hilo")
|
||||
.IsRequired();
|
||||
|
||||
builder.HasDiscriminator<int>("RuleTypeId")
|
||||
.HasValue<UserProfileRule>(RuleType.UserProfileRule.Id)
|
||||
.HasValue<PurchaseHistoryRule>(RuleType.PurchaseHistoryRule.Id)
|
||||
.HasValue<UserLocationRule>(RuleType.UserLocationRule.Id);
|
||||
|
||||
builder.Property(r => r.Description)
|
||||
.HasColumnName("Description")
|
||||
.IsRequired();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class UserLocationRuleEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<UserLocationRule>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<UserLocationRule> builder)
|
||||
{
|
||||
builder.Property(r => r.LocationId)
|
||||
.HasColumnName("LocationId")
|
||||
.IsRequired();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure
|
||||
{
|
||||
using EntityConfigurations;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
|
||||
|
||||
public class MarketingContext : DbContext
|
||||
@ -16,72 +16,9 @@
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
builder.Entity<Campaign>(ConfigureCampaigns);
|
||||
builder.Entity<Rule>(ConfigureRules);
|
||||
builder.Entity<UserLocationRule>(ConfigureUserLocationRules);
|
||||
}
|
||||
|
||||
void ConfigureCampaigns(EntityTypeBuilder<Campaign> builder)
|
||||
{
|
||||
builder.ToTable("Campaign");
|
||||
|
||||
builder.HasKey(m => m.Id);
|
||||
|
||||
builder.Property(m => m.Id)
|
||||
.ForSqlServerUseSequenceHiLo("campaign_hilo")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.Name)
|
||||
.HasColumnName("Name")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.From)
|
||||
.HasColumnName("From")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.To)
|
||||
.HasColumnName("To")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.Description)
|
||||
.HasColumnName("Description")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(m => m.PictureUri)
|
||||
.HasColumnName("PictureUri")
|
||||
.IsRequired();
|
||||
|
||||
builder.HasMany(m => m.Rules)
|
||||
.WithOne(r => r.Campaign)
|
||||
.HasForeignKey(r => r.CampaignId)
|
||||
.IsRequired();
|
||||
}
|
||||
|
||||
void ConfigureRules(EntityTypeBuilder<Rule> builder)
|
||||
{
|
||||
builder.ToTable("Rule");
|
||||
|
||||
builder.HasKey(r => r.Id);
|
||||
|
||||
builder.Property(r => r.Id)
|
||||
.ForSqlServerUseSequenceHiLo("rule_hilo")
|
||||
.IsRequired();
|
||||
|
||||
builder.HasDiscriminator<int>("RuleTypeId")
|
||||
.HasValue<UserProfileRule>(RuleType.UserProfileRule.Id)
|
||||
.HasValue<PurchaseHistoryRule>(RuleType.PurchaseHistoryRule.Id)
|
||||
.HasValue<UserLocationRule>(RuleType.UserLocationRule.Id);
|
||||
|
||||
builder.Property(r => r.Description)
|
||||
.HasColumnName("Description")
|
||||
.IsRequired();
|
||||
}
|
||||
|
||||
void ConfigureUserLocationRules(EntityTypeBuilder<UserLocationRule> builder)
|
||||
{
|
||||
builder.Property(r => r.LocationId)
|
||||
.HasColumnName("LocationId")
|
||||
.IsRequired();
|
||||
builder.ApplyConfiguration(new CampaignEntityTypeConfiguration());
|
||||
builder.ApplyConfiguration(new RuleEntityTypeConfiguration());
|
||||
builder.ApplyConfiguration(new UserLocationRuleEntityTypeConfiguration());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||
|
||||
namespace Ordering.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class BuyerEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<Buyer>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Buyer> buyerConfiguration)
|
||||
{
|
||||
buyerConfiguration.ToTable("buyers", OrderingContext.DEFAULT_SCHEMA);
|
||||
|
||||
buyerConfiguration.HasKey(b => b.Id);
|
||||
|
||||
buyerConfiguration.Ignore(b => b.DomainEvents);
|
||||
|
||||
buyerConfiguration.Property(b => b.Id)
|
||||
.ForSqlServerUseSequenceHiLo("buyerseq", OrderingContext.DEFAULT_SCHEMA);
|
||||
|
||||
buyerConfiguration.Property(b => b.IdentityGuid)
|
||||
.HasMaxLength(200)
|
||||
.IsRequired();
|
||||
|
||||
buyerConfiguration.HasIndex("IdentityGuid")
|
||||
.IsUnique(true);
|
||||
|
||||
buyerConfiguration.HasMany(b => b.PaymentMethods)
|
||||
.WithOne()
|
||||
.HasForeignKey("BuyerId")
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
var navigation = buyerConfiguration.Metadata.FindNavigation(nameof(Buyer.PaymentMethods));
|
||||
|
||||
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||
|
||||
namespace Ordering.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class CardTypeEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<CardType>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<CardType> cardTypesConfiguration)
|
||||
{
|
||||
cardTypesConfiguration.ToTable("cardtypes", OrderingContext.DEFAULT_SCHEMA);
|
||||
|
||||
cardTypesConfiguration.HasKey(ct => ct.Id);
|
||||
|
||||
cardTypesConfiguration.Property(ct => ct.Id)
|
||||
.HasDefaultValue(1)
|
||||
.ValueGeneratedNever()
|
||||
.IsRequired();
|
||||
|
||||
cardTypesConfiguration.Property(ct => ct.Name)
|
||||
.HasMaxLength(200)
|
||||
.IsRequired();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||
|
||||
namespace Ordering.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class ClientRequestEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<ClientRequest>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<ClientRequest> requestConfiguration)
|
||||
{
|
||||
requestConfiguration.ToTable("requests", OrderingContext.DEFAULT_SCHEMA);
|
||||
requestConfiguration.HasKey(cr => cr.Id);
|
||||
requestConfiguration.Property(cr => cr.Name).IsRequired();
|
||||
requestConfiguration.Property(cr => cr.Time).IsRequired();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
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.Infrastructure;
|
||||
using System;
|
||||
|
||||
namespace Ordering.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class OrderEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<Order>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Order> orderConfiguration)
|
||||
{
|
||||
orderConfiguration.ToTable("orders", OrderingContext.DEFAULT_SCHEMA);
|
||||
|
||||
orderConfiguration.HasKey(o => o.Id);
|
||||
|
||||
orderConfiguration.Ignore(b => b.DomainEvents);
|
||||
|
||||
orderConfiguration.Property(o => o.Id)
|
||||
.ForSqlServerUseSequenceHiLo("orderseq", OrderingContext.DEFAULT_SCHEMA);
|
||||
|
||||
orderConfiguration.OwnsOne(o => o.Address);
|
||||
|
||||
orderConfiguration.Property<DateTime>("OrderDate").IsRequired();
|
||||
orderConfiguration.Property<int?>("BuyerId").IsRequired(false);
|
||||
orderConfiguration.Property<int>("OrderStatusId").IsRequired();
|
||||
orderConfiguration.Property<int?>("PaymentMethodId").IsRequired(false);
|
||||
orderConfiguration.Property<string>("Description").IsRequired(false);
|
||||
|
||||
var navigation = orderConfiguration.Metadata.FindNavigation(nameof(Order.OrderItems));
|
||||
|
||||
// DDD Patterns comment:
|
||||
//Set as Field (New since EF 1.1) to access the OrderItem collection property through its field
|
||||
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
|
||||
|
||||
orderConfiguration.HasOne<PaymentMethod>()
|
||||
.WithMany()
|
||||
.HasForeignKey("PaymentMethodId")
|
||||
.IsRequired(false)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
orderConfiguration.HasOne<Buyer>()
|
||||
.WithMany()
|
||||
.IsRequired(false)
|
||||
.HasForeignKey("BuyerId");
|
||||
|
||||
orderConfiguration.HasOne(o => o.OrderStatus)
|
||||
.WithMany()
|
||||
.HasForeignKey("OrderStatusId");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||
|
||||
namespace Ordering.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class OrderItemEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<OrderItem>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<OrderItem> orderItemConfiguration)
|
||||
{
|
||||
orderItemConfiguration.ToTable("orderItems", OrderingContext.DEFAULT_SCHEMA);
|
||||
|
||||
orderItemConfiguration.HasKey(o => o.Id);
|
||||
|
||||
orderItemConfiguration.Ignore(b => b.DomainEvents);
|
||||
|
||||
orderItemConfiguration.Property(o => o.Id)
|
||||
.ForSqlServerUseSequenceHiLo("orderitemseq");
|
||||
|
||||
orderItemConfiguration.Property<int>("OrderId")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<decimal>("Discount")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<int>("ProductId")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<string>("ProductName")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<decimal>("UnitPrice")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<int>("Units")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<string>("PictureUrl")
|
||||
.IsRequired(false);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||
|
||||
namespace Ordering.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class OrderStatusEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<OrderStatus>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<OrderStatus> orderStatusConfiguration)
|
||||
{
|
||||
orderStatusConfiguration.ToTable("orderstatus", OrderingContext.DEFAULT_SCHEMA);
|
||||
|
||||
orderStatusConfiguration.HasKey(o => o.Id);
|
||||
|
||||
orderStatusConfiguration.Property(o => o.Id)
|
||||
.HasDefaultValue(1)
|
||||
.ValueGeneratedNever()
|
||||
.IsRequired();
|
||||
|
||||
orderStatusConfiguration.Property(o => o.Name)
|
||||
.HasMaxLength(200)
|
||||
.IsRequired();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||
using System;
|
||||
|
||||
namespace Ordering.Infrastructure.EntityConfigurations
|
||||
{
|
||||
class PaymentMethodEntityTypeConfiguration
|
||||
: IEntityTypeConfiguration<PaymentMethod>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PaymentMethod> paymentConfiguration)
|
||||
{
|
||||
paymentConfiguration.ToTable("paymentmethods", OrderingContext.DEFAULT_SCHEMA);
|
||||
|
||||
paymentConfiguration.HasKey(b => b.Id);
|
||||
|
||||
paymentConfiguration.Ignore(b => b.DomainEvents);
|
||||
|
||||
paymentConfiguration.Property(b => b.Id)
|
||||
.ForSqlServerUseSequenceHiLo("paymentseq", OrderingContext.DEFAULT_SCHEMA);
|
||||
|
||||
paymentConfiguration.Property<int>("BuyerId")
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<string>("CardHolderName")
|
||||
.HasMaxLength(200)
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<string>("Alias")
|
||||
.HasMaxLength(200)
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<string>("CardNumber")
|
||||
.HasMaxLength(25)
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<DateTime>("Expiration")
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<int>("CardTypeId")
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.HasOne(p => p.CardType)
|
||||
.WithMany()
|
||||
.HasForeignKey("CardTypeId");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,10 @@
|
||||
using MediatR;
|
||||
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 Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
||||
using Ordering.Infrastructure;
|
||||
using Ordering.Infrastructure.EntityConfigurations;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@ -17,7 +15,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
||||
: DbContext,IUnitOfWork
|
||||
|
||||
{
|
||||
const string DEFAULT_SCHEMA = "ordering";
|
||||
public const string DEFAULT_SCHEMA = "ordering";
|
||||
|
||||
public DbSet<Order> Orders { get; set; }
|
||||
|
||||
@ -40,6 +38,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
||||
}
|
||||
|
||||
private OrderingContext(DbContextOptions options) : base (options) { }
|
||||
|
||||
public OrderingContext(DbContextOptions options, IMediator mediator) : base(options)
|
||||
{
|
||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||
@ -50,193 +49,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
|
||||
modelBuilder.Entity<ClientRequest>(ConfigureRequests);
|
||||
modelBuilder.Entity<PaymentMethod>(ConfigurePayment);
|
||||
modelBuilder.Entity<Order>(ConfigureOrder);
|
||||
modelBuilder.Entity<OrderItem>(ConfigureOrderItems);
|
||||
modelBuilder.Entity<CardType>(ConfigureCardTypes);
|
||||
modelBuilder.Entity<OrderStatus>(ConfigureOrderStatus);
|
||||
modelBuilder.Entity<Buyer>(ConfigureBuyer);
|
||||
}
|
||||
|
||||
private void ConfigureRequests(EntityTypeBuilder<ClientRequest> requestConfiguration)
|
||||
{
|
||||
requestConfiguration.ToTable("requests", DEFAULT_SCHEMA);
|
||||
requestConfiguration.HasKey(cr => cr.Id);
|
||||
requestConfiguration.Property(cr => cr.Name).IsRequired();
|
||||
requestConfiguration.Property(cr => cr.Time).IsRequired();
|
||||
}
|
||||
|
||||
|
||||
void ConfigureBuyer(EntityTypeBuilder<Buyer> buyerConfiguration)
|
||||
{
|
||||
buyerConfiguration.ToTable("buyers", DEFAULT_SCHEMA);
|
||||
|
||||
buyerConfiguration.HasKey(b => b.Id);
|
||||
|
||||
buyerConfiguration.Ignore(b => b.DomainEvents);
|
||||
|
||||
buyerConfiguration.Property(b => b.Id)
|
||||
.ForSqlServerUseSequenceHiLo("buyerseq", DEFAULT_SCHEMA);
|
||||
|
||||
buyerConfiguration.Property(b=>b.IdentityGuid)
|
||||
.HasMaxLength(200)
|
||||
.IsRequired();
|
||||
|
||||
buyerConfiguration.HasIndex("IdentityGuid")
|
||||
.IsUnique(true);
|
||||
|
||||
buyerConfiguration.HasMany(b => b.PaymentMethods)
|
||||
.WithOne()
|
||||
.HasForeignKey("BuyerId")
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
var navigation = buyerConfiguration.Metadata.FindNavigation(nameof(Buyer.PaymentMethods));
|
||||
|
||||
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
|
||||
}
|
||||
|
||||
void ConfigurePayment(EntityTypeBuilder<PaymentMethod> paymentConfiguration)
|
||||
{
|
||||
paymentConfiguration.ToTable("paymentmethods", DEFAULT_SCHEMA);
|
||||
|
||||
paymentConfiguration.HasKey(b => b.Id);
|
||||
|
||||
paymentConfiguration.Ignore(b => b.DomainEvents);
|
||||
|
||||
paymentConfiguration.Property(b => b.Id)
|
||||
.ForSqlServerUseSequenceHiLo("paymentseq", DEFAULT_SCHEMA);
|
||||
|
||||
paymentConfiguration.Property<int>("BuyerId")
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<string>("CardHolderName")
|
||||
.HasMaxLength(200)
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<string>("Alias")
|
||||
.HasMaxLength(200)
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<string>("CardNumber")
|
||||
.HasMaxLength(25)
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<DateTime>("Expiration")
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.Property<int>("CardTypeId")
|
||||
.IsRequired();
|
||||
|
||||
paymentConfiguration.HasOne(p => p.CardType)
|
||||
.WithMany()
|
||||
.HasForeignKey("CardTypeId");
|
||||
}
|
||||
|
||||
void ConfigureOrder(EntityTypeBuilder<Order> orderConfiguration)
|
||||
{
|
||||
orderConfiguration.ToTable("orders", DEFAULT_SCHEMA);
|
||||
|
||||
orderConfiguration.HasKey(o => o.Id);
|
||||
|
||||
orderConfiguration.Ignore(b => b.DomainEvents);
|
||||
|
||||
orderConfiguration.Property(o => o.Id)
|
||||
.ForSqlServerUseSequenceHiLo("orderseq", DEFAULT_SCHEMA);
|
||||
|
||||
orderConfiguration.OwnsOne(o => o.Address);
|
||||
|
||||
orderConfiguration.Property<DateTime>("OrderDate").IsRequired();
|
||||
orderConfiguration.Property<int?>("BuyerId").IsRequired(false);
|
||||
orderConfiguration.Property<int>("OrderStatusId").IsRequired();
|
||||
orderConfiguration.Property<int?>("PaymentMethodId").IsRequired(false);
|
||||
orderConfiguration.Property<string>("Description").IsRequired(false);
|
||||
|
||||
var navigation = orderConfiguration.Metadata.FindNavigation(nameof(Order.OrderItems));
|
||||
// DDD Patterns comment:
|
||||
//Set as Field (New since EF 1.1) to access the OrderItem collection property through its field
|
||||
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
|
||||
|
||||
orderConfiguration.HasOne<PaymentMethod>()
|
||||
.WithMany()
|
||||
.HasForeignKey("PaymentMethodId")
|
||||
.IsRequired(false)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
orderConfiguration.HasOne<Buyer>()
|
||||
.WithMany()
|
||||
.IsRequired(false)
|
||||
.HasForeignKey("BuyerId");
|
||||
|
||||
orderConfiguration.HasOne(o => o.OrderStatus)
|
||||
.WithMany()
|
||||
.HasForeignKey("OrderStatusId");
|
||||
}
|
||||
|
||||
void ConfigureOrderItems(EntityTypeBuilder<OrderItem> orderItemConfiguration)
|
||||
{
|
||||
orderItemConfiguration.ToTable("orderItems", DEFAULT_SCHEMA);
|
||||
|
||||
orderItemConfiguration.HasKey(o => o.Id);
|
||||
|
||||
orderItemConfiguration.Ignore(b => b.DomainEvents);
|
||||
|
||||
orderItemConfiguration.Property(o => o.Id)
|
||||
.ForSqlServerUseSequenceHiLo("orderitemseq");
|
||||
|
||||
orderItemConfiguration.Property<int>("OrderId")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<decimal>("Discount")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<int>("ProductId")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<string>("ProductName")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<decimal>("UnitPrice")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<int>("Units")
|
||||
.IsRequired();
|
||||
|
||||
orderItemConfiguration.Property<string>("PictureUrl")
|
||||
.IsRequired(false);
|
||||
}
|
||||
|
||||
void ConfigureOrderStatus(EntityTypeBuilder<OrderStatus> orderStatusConfiguration)
|
||||
{
|
||||
orderStatusConfiguration.ToTable("orderstatus", DEFAULT_SCHEMA);
|
||||
|
||||
orderStatusConfiguration.HasKey(o => o.Id);
|
||||
|
||||
orderStatusConfiguration.Property(o => o.Id)
|
||||
.HasDefaultValue(1)
|
||||
.ValueGeneratedNever()
|
||||
.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)
|
||||
.ValueGeneratedNever()
|
||||
.IsRequired();
|
||||
|
||||
cardTypesConfiguration.Property(ct => ct.Name)
|
||||
.HasMaxLength(200)
|
||||
.IsRequired();
|
||||
modelBuilder.ApplyConfiguration(new ClientRequestEntityTypeConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new PaymentMethodEntityTypeConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new OrderEntityTypeConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new OrderItemEntityTypeConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new CardTypeEntityTypeConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new OrderStatusEntityTypeConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new BuyerEntityTypeConfiguration());
|
||||
}
|
||||
|
||||
public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken))
|
||||
|
Loading…
x
Reference in New Issue
Block a user