diff --git a/N.EntityFrameworkCore.Extensions.Test/Data/Address.cs b/N.EntityFrameworkCore.Extensions.Test/Data/Address.cs new file mode 100644 index 0000000..aca92bd --- /dev/null +++ b/N.EntityFrameworkCore.Extensions.Test/Data/Address.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace N.EntityFrameworkCore.Extensions.Test.Data; +[ComplexType] +public class Address +{ + public required string Line1 { get; set; } + public string? Line2 { get; set; } + public required string City { get; set; } + public required string Country { get; set; } + public required string PostCode { get; set; } +} diff --git a/N.EntityFrameworkCore.Extensions.Test/Data/OrderWithComplexType.cs b/N.EntityFrameworkCore.Extensions.Test/Data/OrderWithComplexType.cs new file mode 100644 index 0000000..24fc21b --- /dev/null +++ b/N.EntityFrameworkCore.Extensions.Test/Data/OrderWithComplexType.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace N.EntityFrameworkCore.Extensions.Test.Data; +public class OrderWithComplexType +{ + [Key] + public long Id { get; set; } + [Required] + public Address ShippingAddress { get; set; } + [Required] + public Address BillingAddress { get; set; } +} diff --git a/N.EntityFrameworkCore.Extensions.Test/Data/TestDbContext.cs b/N.EntityFrameworkCore.Extensions.Test/Data/TestDbContext.cs index b564256..1aba577 100644 --- a/N.EntityFrameworkCore.Extensions.Test/Data/TestDbContext.cs +++ b/N.EntityFrameworkCore.Extensions.Test/Data/TestDbContext.cs @@ -13,6 +13,7 @@ public class TestDbContext : DbContext public virtual DbSet ProductsWithComplexKey { get; set; } public virtual DbSet ProductsWithTrigger { get; set; } public virtual DbSet Orders { get; set; } + public virtual DbSet OrdersWithComplexType { get; set; } public virtual DbSet TpcPeople { get; set; } public virtual DbSet TphPeople { get; set; } public virtual DbSet TphCustomers { get; set; } @@ -38,6 +39,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity().Property("DbModifiedDateTime").HasComputedColumnSql("getdate()"); modelBuilder.Entity().Property(p => p.DbActive).HasDefaultValueSql("((1))"); modelBuilder.Entity().Property(p => p.Status).HasConversion(); + modelBuilder.Entity(b => + { + b.ComplexProperty(e => e.BillingAddress); + b.ComplexProperty(e => e.ShippingAddress); + }); modelBuilder.Entity().UseTpcMappingStrategy(); modelBuilder.Entity().ToTable("TpcCustomer"); modelBuilder.Entity().ToTable("TpcVendor"); diff --git a/N.EntityFrameworkCore.Extensions.Test/DbContextExtensions/BulkInsert.cs b/N.EntityFrameworkCore.Extensions.Test/DbContextExtensions/BulkInsert.cs index 81b2b01..3e3824a 100644 --- a/N.EntityFrameworkCore.Extensions.Test/DbContextExtensions/BulkInsert.cs +++ b/N.EntityFrameworkCore.Extensions.Test/DbContextExtensions/BulkInsert.cs @@ -29,6 +29,38 @@ public void With_Complex_Key() Assert.IsTrue(newTotal - oldTotal == rowsInserted, "The new count minus the old count should match the number of rows inserted."); } [TestMethod] + public void With_Complex_Type() + { + var dbContext = SetupDbContext(true); + var orders = new List(); + for (int i = 1; i < 1000; i++) + { + orders.Add(new OrderWithComplexType { + Id = i, + ShippingAddress = new Address + { + Line1 = $"123 Main St, {i}", + City = "Atlanta", + Country = "USA", + PostCode = "30303" + }, + BillingAddress = new Address + { + Line1 = $"456 Oak St, {i}", + City = "Atlanta", + Country = "USA", + PostCode = "30303" + } + }); + } + int oldTotal = dbContext.OrdersWithComplexType.Count(); + int rowsInserted = dbContext.BulkInsert(orders); + int newTotal = dbContext.OrdersWithComplexType.Count(); + + Assert.IsTrue(rowsInserted == orders.Count, "The number of rows inserted must match the count of order list"); + Assert.IsTrue(newTotal - oldTotal == rowsInserted, "The new count minus the old count should match the number of rows inserted."); + } + [TestMethod] public void With_Default_Options() { var dbContext = SetupDbContext(false);