Skip to content

Commit

Permalink
Updated packages; Using configuration classes for EF Core.
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Aug 18, 2019
1 parent 6eba16b commit 305be55
Show file tree
Hide file tree
Showing 21 changed files with 599 additions and 153 deletions.
6 changes: 3 additions & 3 deletions eShopOnWeb.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.16
# Visual Studio Version 16
VisualStudioVersion = 16.0.29102.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{419A6ACE-0419-4315-A6FB-B0E63D39432E}"
EndProject
Expand All @@ -23,6 +22,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0BD72BEA-EF42-4B72-8B69-12A39EC76FBA}"
ProjectSection(SolutionItems) = preProject
docker-compose.yml = docker-compose.yml
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Web", "src\Web\Web.csproj", "{227CF035-29B0-448D-97E4-944F9EA850E5}"
Expand Down
2 changes: 1 addition & 1 deletion src/ApplicationCore/ApplicationCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="1.2.8" />
<PackageReference Include="Ardalis.GuardClauses" Version="1.2.9" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
</ItemGroup>

Expand Down
136 changes: 6 additions & 130 deletions src/Infrastructure/Data/CatalogContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Ardalis.EFCore.Extensions;
using Microsoft.EntityFrameworkCore;
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<CatalogContext> options) : base(options)
Expand All @@ -23,134 +22,11 @@ public CatalogContext(DbContextOptions<CatalogContext> options) : base(options)

protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Basket>(ConfigureBasket);
builder.Entity<CatalogBrand>(ConfigureCatalogBrand);
builder.Entity<CatalogType>(ConfigureCatalogType);
builder.Entity<CatalogItem>(ConfigureCatalogItem);
builder.Entity<Order>(ConfigureOrder);
builder.Entity<OrderItem>(ConfigureOrderItem);
builder.Entity<Address>(ConfigureAddress);
builder.Entity<CatalogItemOrdered>(ConfigureCatalogItemOrdered);
builder.Entity<BasketItem>(ConfigureBasketItem);
}

private void ConfigureBasketItem(EntityTypeBuilder<BasketItem> builder)
{
builder.Property(bi => bi.UnitPrice)
.IsRequired(true)
.HasColumnType("decimal(18,2)");
}

private void ConfigureCatalogItemOrdered(EntityTypeBuilder<CatalogItemOrdered> builder)
{
builder.Property(cio => cio.ProductName)
.HasMaxLength(50)
.IsRequired();
}

private void ConfigureAddress(EntityTypeBuilder<Address> 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<Basket> builder)
{
var navigation = builder.Metadata.FindNavigation(nameof(Basket.Items));

navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
}

private 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)
.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<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);
}

private 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);
}

private void ConfigureOrder(EntityTypeBuilder<Order> builder)
{
var navigation = builder.Metadata.FindNavigation(nameof(Order.OrderItems));

navigation.SetPropertyAccessMode(PropertyAccessMode.Field);

builder.OwnsOne(o => o.ShipToAddress);
}

private void ConfigureOrderItem(EntityTypeBuilder<OrderItem> builder)
{
builder.OwnsOne(i => i.ItemOrdered);
base.OnModelCreating(builder);
builder.ApplyAllConfigurationsFromCurrentAssembly();

builder.Property(oi => oi.UnitPrice)
.IsRequired(true)
.HasColumnType("decimal(18,2)");
// alternately this is built-in to EF Core 2.2
//builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
}
}
31 changes: 31 additions & 0 deletions src/Infrastructure/Data/Config/AddressConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;

namespace Microsoft.eShopWeb.Infrastructure.Data.Config
{
public class AddressConfiguration : IEntityTypeConfiguration<Address>
{
public void Configure(EntityTypeBuilder<Address> 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();
}
}
}
19 changes: 19 additions & 0 deletions src/Infrastructure/Data/Config/BasketConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;

namespace Microsoft.eShopWeb.Infrastructure.Data.Config
{
public class BasketConfiguration : IEntityTypeConfiguration<Basket>
{
public void Configure(EntityTypeBuilder<Basket> builder)
{
var navigation = builder.Metadata.FindNavigation(nameof(Basket.Items));
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);

builder.Property(b => b.BuyerId)
.IsRequired()
.HasMaxLength(20);
}
}
}
16 changes: 16 additions & 0 deletions src/Infrastructure/Data/Config/BasketItemConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;

namespace Microsoft.eShopWeb.Infrastructure.Data.Config
{
public class BasketItemConfiguration : IEntityTypeConfiguration<BasketItem>
{
public void Configure(EntityTypeBuilder<BasketItem> builder)
{
builder.Property(bi => bi.UnitPrice)
.IsRequired(true)
.HasColumnType("decimal(18,2)");
}
}
}
22 changes: 22 additions & 0 deletions src/Infrastructure/Data/Config/CatalogBrandConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities;

namespace Microsoft.eShopWeb.Infrastructure.Data.Config
{
public class CatalogBrandConfiguration : IEntityTypeConfiguration<CatalogBrand>
{
public void Configure(EntityTypeBuilder<CatalogBrand> builder)
{
builder.HasKey(ci => ci.Id);

builder.Property(ci => ci.Id)
.ForSqlServerUseSequenceHiLo("catalog_brand_hilo")
.IsRequired();

builder.Property(cb => cb.Brand)
.IsRequired()
.HasMaxLength(100);
}
}
}
37 changes: 37 additions & 0 deletions src/Infrastructure/Data/Config/CatalogItemConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities;

namespace Microsoft.eShopWeb.Infrastructure.Data.Config
{
public class CatalogItemConfiguration : 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)
.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);
}
}
}
16 changes: 16 additions & 0 deletions src/Infrastructure/Data/Config/CatalogItemOrderedConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;

namespace Microsoft.eShopWeb.Infrastructure.Data.Config
{
public class CatalogItemOrderedConfiguration : IEntityTypeConfiguration<CatalogItemOrdered>
{
public void Configure(EntityTypeBuilder<CatalogItemOrdered> builder)
{
builder.Property(cio => cio.ProductName)
.HasMaxLength(50)
.IsRequired();
}
}
}
22 changes: 22 additions & 0 deletions src/Infrastructure/Data/Config/CatalogTypeConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities;

namespace Microsoft.eShopWeb.Infrastructure.Data.Config
{
public class CatalogTypeConfiguration : IEntityTypeConfiguration<CatalogType>
{
public void Configure(EntityTypeBuilder<CatalogType> builder)
{
builder.HasKey(ci => ci.Id);

builder.Property(ci => ci.Id)
.ForSqlServerUseSequenceHiLo("catalog_type_hilo")
.IsRequired();

builder.Property(cb => cb.Type)
.IsRequired()
.HasMaxLength(100);
}
}
}
18 changes: 18 additions & 0 deletions src/Infrastructure/Data/Config/OrderConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;

namespace Microsoft.eShopWeb.Infrastructure.Data.Config
{
public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
var navigation = builder.Metadata.FindNavigation(nameof(Order.OrderItems));

navigation.SetPropertyAccessMode(PropertyAccessMode.Field);

builder.OwnsOne(o => o.ShipToAddress);
}
}
}
18 changes: 18 additions & 0 deletions src/Infrastructure/Data/Config/OrderItemConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;

namespace Microsoft.eShopWeb.Infrastructure.Data.Config
{
public class OrderItemConfiguration : IEntityTypeConfiguration<OrderItem>
{
public void Configure(EntityTypeBuilder<OrderItem> builder)
{
builder.OwnsOne(i => i.ItemOrdered);

builder.Property(oi => oi.UnitPrice)
.IsRequired(true)
.HasColumnType("decimal(18,2)");
}
}
}
Loading

0 comments on commit 305be55

Please sign in to comment.