diff --git a/samples/apis/Backet.Api/Backet.Api.csproj b/samples/apis/Backet.Api/Backet.Api.csproj index a084f9d..4a5489f 100644 --- a/samples/apis/Backet.Api/Backet.Api.csproj +++ b/samples/apis/Backet.Api/Backet.Api.csproj @@ -9,6 +9,12 @@ + + + + + + diff --git a/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/Backet.cs b/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/Backet.cs index 337d40f..b716eb7 100644 --- a/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/Backet.cs +++ b/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/Backet.cs @@ -9,7 +9,7 @@ namespace Backet.Api.Domain.AggregatesModel.BacketAggregate public class Backet: Entity,IAggregateRoot { public string UserId { get; set; } - public IReadOnlyCollection BacketItems { get; private set; } + public IEnumerable BacketItems { get; private set; } public long TotalPrice { get; set; } } } diff --git a/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/BacketItem.cs b/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/BacketItem.cs index d9c54ae..f9953f5 100644 --- a/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/BacketItem.cs +++ b/samples/apis/Backet.Api/Domain/AggregatesModel/BacketAggregate/BacketItem.cs @@ -11,5 +11,6 @@ namespace Backet.Api.Domain.AggregatesModel.BacketAggregate public string ProductId { get; set; } public string ProductName { get; set; } public long Price { get; set; } + public string BacketId { get; set; } } } diff --git a/samples/apis/Backet.Api/Infrastructure/EntityConfigurations/BacketEntityTypeConfiguration.cs b/samples/apis/Backet.Api/Infrastructure/EntityConfigurations/BacketEntityTypeConfiguration.cs index f4e5996..282a6cd 100644 --- a/samples/apis/Backet.Api/Infrastructure/EntityConfigurations/BacketEntityTypeConfiguration.cs +++ b/samples/apis/Backet.Api/Infrastructure/EntityConfigurations/BacketEntityTypeConfiguration.cs @@ -15,7 +15,7 @@ namespace Backet.Api.Infrastructure.EntityConfigurations builder.Property(m => m.Id).HasMaxLength(32); builder.Property(m => m.UserId).HasMaxLength(32).IsRequired(); - builder.HasMany(m => m.BacketItems).WithOne(); + builder.HasMany(m => m.BacketItems).WithOne().IsRequired().OnDelete(DeleteBehavior.Cascade).HasForeignKey("BacketId"); builder.Ignore(m => m.DomainEvents); diff --git a/samples/apis/Backet.Api/Infrastructure/EntityConfigurations/BacketItemEntityTypeConfiguration.cs b/samples/apis/Backet.Api/Infrastructure/EntityConfigurations/BacketItemEntityTypeConfiguration.cs index 51dff27..0d97721 100644 --- a/samples/apis/Backet.Api/Infrastructure/EntityConfigurations/BacketItemEntityTypeConfiguration.cs +++ b/samples/apis/Backet.Api/Infrastructure/EntityConfigurations/BacketItemEntityTypeConfiguration.cs @@ -17,6 +17,7 @@ namespace Backet.Api.Infrastructure.EntityConfigurations builder.Property(m => m.Id).HasMaxLength(32); builder.Property(m => m.ProductId).HasMaxLength(32); builder.Property(m => m.ProductName).HasMaxLength(256).IsRequired(); + builder.Property(m => m.BacketId).HasMaxLength(32).IsRequired(); builder.Ignore(m => m.DomainEvents); diff --git a/samples/apis/Backet.Api/Migrations/20200119013821_Remove_BacketItem_FK_Constraint.Designer.cs b/samples/apis/Backet.Api/Migrations/20200119013821_Remove_BacketItem_FK_Constraint.Designer.cs new file mode 100644 index 0000000..4d17eb9 --- /dev/null +++ b/samples/apis/Backet.Api/Migrations/20200119013821_Remove_BacketItem_FK_Constraint.Designer.cs @@ -0,0 +1,86 @@ +// +using Backet.Api.Infrastructure; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace Backet.Api.Migrations +{ + [DbContext(typeof(BacketDbContext))] + [Migration("20200119013821_Remove_BacketItem_FK_Constraint")] + partial class Remove_BacketItem_FK_Constraint + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) + .HasAnnotation("ProductVersion", "3.1.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + modelBuilder.Entity("Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet", b => + { + b.Property("Id") + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.Property("TotalPrice") + .HasColumnType("bigint"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Backet"); + }); + + modelBuilder.Entity("Backet.Api.Domain.AggregatesModel.BacketAggregate.BacketItem", b => + { + b.Property("Id") + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.Property("BacketId") + .IsRequired() + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.Property("Price") + .HasColumnType("bigint"); + + b.Property("ProductId") + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("character varying(256)") + .HasMaxLength(256); + + b.HasKey("Id"); + + b.HasIndex("BacketId"); + + b.HasIndex("ProductId"); + + b.ToTable("BacketItem"); + }); + + modelBuilder.Entity("Backet.Api.Domain.AggregatesModel.BacketAggregate.BacketItem", b => + { + b.HasOne("Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet", null) + .WithMany("BacketItems") + .HasForeignKey("BacketId") + .OnDelete(DeleteBehavior.Restrict); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/samples/apis/Backet.Api/Migrations/20200119013821_Remove_BacketItem_FK_Constraint.cs b/samples/apis/Backet.Api/Migrations/20200119013821_Remove_BacketItem_FK_Constraint.cs new file mode 100644 index 0000000..e1f560b --- /dev/null +++ b/samples/apis/Backet.Api/Migrations/20200119013821_Remove_BacketItem_FK_Constraint.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace Backet.Api.Migrations +{ + public partial class Remove_BacketItem_FK_Constraint : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "BacketId", + table: "BacketItem", + maxLength: 32, + nullable: false, + oldClrType: typeof(string), + oldType: "character varying(32)", + oldNullable: true); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "BacketId", + table: "BacketItem", + type: "character varying(32)", + nullable: true, + oldClrType: typeof(string), + oldMaxLength: 32); + } + } +} diff --git a/samples/apis/Backet.Api/Migrations/20200119020249_Update_BacketItem_FK_Constraint.Designer.cs b/samples/apis/Backet.Api/Migrations/20200119020249_Update_BacketItem_FK_Constraint.Designer.cs new file mode 100644 index 0000000..446f16c --- /dev/null +++ b/samples/apis/Backet.Api/Migrations/20200119020249_Update_BacketItem_FK_Constraint.Designer.cs @@ -0,0 +1,87 @@ +// +using Backet.Api.Infrastructure; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace Backet.Api.Migrations +{ + [DbContext(typeof(BacketDbContext))] + [Migration("20200119020249_Update_BacketItem_FK_Constraint")] + partial class Update_BacketItem_FK_Constraint + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) + .HasAnnotation("ProductVersion", "3.1.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + modelBuilder.Entity("Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet", b => + { + b.Property("Id") + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.Property("TotalPrice") + .HasColumnType("bigint"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Backet"); + }); + + modelBuilder.Entity("Backet.Api.Domain.AggregatesModel.BacketAggregate.BacketItem", b => + { + b.Property("Id") + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.Property("BacketId") + .IsRequired() + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.Property("Price") + .HasColumnType("bigint"); + + b.Property("ProductId") + .HasColumnType("character varying(32)") + .HasMaxLength(32); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("character varying(256)") + .HasMaxLength(256); + + b.HasKey("Id"); + + b.HasIndex("BacketId"); + + b.HasIndex("ProductId"); + + b.ToTable("BacketItem"); + }); + + modelBuilder.Entity("Backet.Api.Domain.AggregatesModel.BacketAggregate.BacketItem", b => + { + b.HasOne("Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet", null) + .WithMany("BacketItems") + .HasForeignKey("BacketId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/samples/apis/Backet.Api/Migrations/20200119020249_Update_BacketItem_FK_Constraint.cs b/samples/apis/Backet.Api/Migrations/20200119020249_Update_BacketItem_FK_Constraint.cs new file mode 100644 index 0000000..0e70f20 --- /dev/null +++ b/samples/apis/Backet.Api/Migrations/20200119020249_Update_BacketItem_FK_Constraint.cs @@ -0,0 +1,37 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace Backet.Api.Migrations +{ + public partial class Update_BacketItem_FK_Constraint : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_BacketItem_Backet_BacketId", + table: "BacketItem"); + + migrationBuilder.AddForeignKey( + name: "FK_BacketItem_Backet_BacketId", + table: "BacketItem", + column: "BacketId", + principalTable: "Backet", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_BacketItem_Backet_BacketId", + table: "BacketItem"); + + migrationBuilder.AddForeignKey( + name: "FK_BacketItem_Backet_BacketId", + table: "BacketItem", + column: "BacketId", + principalTable: "Backet", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + } +} diff --git a/samples/apis/Backet.Api/Migrations/BacketDbContextModelSnapshot.cs b/samples/apis/Backet.Api/Migrations/BacketDbContextModelSnapshot.cs index a6b706d..50ee5b9 100644 --- a/samples/apis/Backet.Api/Migrations/BacketDbContextModelSnapshot.cs +++ b/samples/apis/Backet.Api/Migrations/BacketDbContextModelSnapshot.cs @@ -46,7 +46,9 @@ namespace Backet.Api.Migrations .HasMaxLength(32); b.Property("BacketId") - .HasColumnType("character varying(32)"); + .IsRequired() + .HasColumnType("character varying(32)") + .HasMaxLength(32); b.Property("Price") .HasColumnType("bigint"); @@ -73,7 +75,9 @@ namespace Backet.Api.Migrations { b.HasOne("Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet", null) .WithMany("BacketItems") - .HasForeignKey("BacketId"); + .HasForeignKey("BacketId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); #pragma warning restore 612, 618 }