Skip to content

Commit

Permalink
Merge pull request #87 from NorthernLight1/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
NorthernLight1 authored Nov 8, 2024
2 parents 68cc6b4 + c8dcecf commit 511a47e
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 35 deletions.
1 change: 1 addition & 0 deletions N.EntityFrameworkCore.Extensions.Test/Data/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Order
public DateTime AddedDateTime { get; set; }
public DateTime? ModifiedDateTime { get; set; }
public DateTimeOffset? ModifiedDateTimeOffset { get; set; }
public bool DbActive { get; set; }
public DateTime DbAddedDateTime { get; set; }
public DateTime DbModifiedDateTime { get; set; }
public bool? Trigger { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class ProductWithComplexKey
public Guid Key1 { get; set; }
public Guid Key2 { get; set; }
public Guid Key3 { get; set; }
public Guid Key4 { get; set; }
public string ExternalId { get; set; }
public decimal Price { get; set; }
public bool OutOfStock { get; set; }
[Column("Status")]
Expand All @@ -17,8 +19,7 @@ public class ProductWithComplexKey
public DateTime? UpdatedDateTime { get; set; }
public ProductWithComplexKey()
{
//Key1 = Guid.NewGuid();
//Key2 = Guid.NewGuid();
//Key3 = Guid.NewGuid();
Key3 = Guid.NewGuid();
Key4 = Guid.NewGuid();
}
}
3 changes: 2 additions & 1 deletion N.EntityFrameworkCore.Extensions.Test/Data/TestDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<ProductWithComplexKey>().HasKey(c => new { c.Key1 });
modelBuilder.Entity<ProductWithComplexKey>().Property<Guid>("Key1").HasDefaultValueSql("newsequentialid()");
modelBuilder.Entity<ProductWithComplexKey>().Property<Guid>("Key2").HasDefaultValueSql("newsequentialid()");
modelBuilder.Entity<ProductWithComplexKey>().Property<Guid>("Key3").HasDefaultValueSql("newsequentialid()");
modelBuilder.Entity<ProductWithComplexKey>().HasKey(p => new { p.Key3, p.Key4 });
modelBuilder.Entity<Order>().Property<DateTime>("DbAddedDateTime").HasDefaultValueSql("getdate()");
modelBuilder.Entity<Order>().Property<DateTime>("DbModifiedDateTime").HasComputedColumnSql("getdate()");
modelBuilder.Entity<Order>().Property<bool>(p => p.DbActive).HasDefaultValueSql("((1))");
modelBuilder.Entity<Order>().Property(p => p.Status).HasConversion<string>();
modelBuilder.Entity<TpcPerson>().UseTpcMappingStrategy();
modelBuilder.Entity<TpcCustomer>().ToTable("TpcCustomer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public void With_ValueGenerated_Default()
}
int oldTotal = dbContext.Orders.Where(o => o.Price <= 10).Count();
int rowsInserted = dbContext.BulkInsert(orders);
int newTotal = dbContext.Orders.Where(o => o.Price <= 10 && o.DbAddedDateTime > nowDateTime).Count();
int newTotal = dbContext.Orders.Where(o => o.Price <= 10 && o.DbAddedDateTime > nowDateTime && o.DbActive).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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public async Task With_ValueGenerated_Default()
}
int oldTotal = dbContext.Orders.Where(o => o.Price <= 10).Count();
int rowsInserted = await dbContext.BulkInsertAsync(orders);
int newTotal = dbContext.Orders.Where(o => o.Price <= 10 && o.DbAddedDateTime > nowDateTime).Count();
int newTotal = dbContext.Orders.Where(o => o.Price <= 10 && o.DbAddedDateTime > nowDateTime && o.DbActive).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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@ namespace N.EntityFrameworkCore.Extensions.Test.DbContextExtensions;
[TestClass]
public class BulkMerge : DbContextExtensionsBase
{
[TestMethod]
public void With_Complex_Key()
{
var dbContext = SetupDbContext(true);
var products = dbContext.ProductsWithComplexKey.Where(o => o.Price == 1.25M).ToList();
int productsToAdd = 5000;
decimal updatedPrice = 5.25M;
var productsToUpdate = products.ToList();
foreach (var product in products)
{
product.Price = updatedPrice;
}
for (int i = 0; i < productsToAdd; i++)
{
products.Add(new ProductWithComplexKey { ExternalId = (20000 + i).ToString(), Price = 3.55M });
}
var result = dbContext.BulkMerge(products);
var allProducts = dbContext.ProductsWithComplexKey.ToList();
bool areAddedOrdersMerged = true;
bool areUpdatedOrdersMerged = true;
foreach (var product in allProducts)
{
if (productsToUpdate.Contains(product) && product.Price != updatedPrice)
{
areUpdatedOrdersMerged = false;
break;
}
}

Assert.IsTrue(result.RowsAffected == products.Count(), "The number of rows inserted must match the count of order list");
Assert.IsTrue(result.RowsUpdated == productsToUpdate.Count, "The number of rows updated must match");
Assert.IsTrue(result.RowsInserted == productsToAdd, "The number of rows added must match");
Assert.IsTrue(areAddedOrdersMerged, "The orders that were added did not merge correctly");
Assert.IsTrue(areUpdatedOrdersMerged, "The orders that were updated did not merge correctly");
}
[TestMethod]
public void With_Default_Options()
{
Expand Down Expand Up @@ -218,32 +253,6 @@ public void With_Options_AutoMapIdentity()
Assert.IsTrue(autoMapIdentityMatched, "The auto mapping of ids of entities that were merged failed to match up");
}
[TestMethod]
public void With_merge_Should_include_all_auto_generated_properties()
{
var dbContext = SetupDbContext(true);
int ordersToUpdate = 3;
int ordersToAdd = 2;
var orders = new List<Order>
{
new Order { ExternalId = "id-1", Price=7.10M },
new Order { ExternalId = "id-2", Price=9.33M },
new Order { ExternalId = "id-3", Price=3.25M },
new Order { ExternalId = "id-1000001", Price=2.15M },
new Order { ExternalId = "id-1000002", Price=5.75M },
};
var result = dbContext.BulkMerge(orders, new BulkMergeOptions<Order>
{
MergeOnCondition = (s, t) => s.ExternalId == t.ExternalId,
AutoMapOutput = true
});
var autoMapIdentityMatched = orders.All(x => x.Id != 0);

Assert.IsTrue(result.RowsAffected == ordersToAdd + ordersToUpdate, "The number of rows inserted must match the count of order list");
Assert.IsTrue(result.RowsUpdated == ordersToUpdate, "The number of rows updated must match");
Assert.IsTrue(result.RowsInserted == ordersToAdd, "The number of rows added must match");
Assert.IsTrue(autoMapIdentityMatched, "The auto mapping of ids of entities that were merged failed to match up");
}
[TestMethod]
public void With_Key()
{
var dbContext = SetupDbContext(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,41 @@ namespace N.EntityFrameworkCore.Extensions.Test.DbContextExtensions;
[TestClass]
public class BulkMergeAsync : DbContextExtensionsBase
{
[TestMethod]
public async Task With_Complex_Key()
{
var dbContext = SetupDbContext(true);
var products = dbContext.ProductsWithComplexKey.Where(o => o.Price == 1.25M).ToList();
int productsToAdd = 5000;
decimal updatedPrice = 5.25M;
var productsToUpdate = products.ToList();
foreach (var product in products)
{
product.Price = updatedPrice;
}
for (int i = 0; i < productsToAdd; i++)
{
products.Add(new ProductWithComplexKey { ExternalId = (20000 + i).ToString(), Price = 3.55M });
}
var result = await dbContext.BulkMergeAsync(products);
var allProducts = dbContext.ProductsWithComplexKey.ToList();
bool areAddedOrdersMerged = true;
bool areUpdatedOrdersMerged = true;
foreach (var product in allProducts)
{
if (productsToUpdate.Contains(product) && product.Price != updatedPrice)
{
areUpdatedOrdersMerged = false;
break;
}
}

Assert.IsTrue(result.RowsAffected == products.Count(), "The number of rows inserted must match the count of order list");
Assert.IsTrue(result.RowsUpdated == productsToUpdate.Count, "The number of rows updated must match");
Assert.IsTrue(result.RowsInserted == productsToAdd, "The number of rows added must match");
Assert.IsTrue(areAddedOrdersMerged, "The orders that were added did not merge correctly");
Assert.IsTrue(areUpdatedOrdersMerged, "The orders that were updated did not merge correctly");
}
[TestMethod]
public async Task With_Default_Options()
{
Expand Down
3 changes: 2 additions & 1 deletion N.EntityFrameworkCore.Extensions/Data/BulkOperation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -138,7 +139,7 @@ .. TableMapping.GetEntityProperties(entityType, ValueGenerated.OnAddOrUpdate).To

private IEnumerable<string> GetMergeOutputColumns(IEnumerable<string> autoGeneratedColumns, bool delete = false)
{
List<string> columnsToOutput = new List<string> { "$Action", string.Format("[{0}].[{1}]", "s", Constants.InternalId_ColumnName) };
List<string> columnsToOutput = new List<string> { "$action", string.Format("[{0}].[{1}]", "s", Constants.InternalId_ColumnName) };
columnsToOutput.AddRange(autoGeneratedColumns.Select(o => string.Format("[inserted].[{0}]", o)));
return columnsToOutput.AsEnumerable();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Version>8.0.0.11</Version>
<Version>8.0.0.12</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/NorthernLight1/N.EntityFrameworkCore.Extensions/</PackageProjectUrl>
<Authors>Northern25</Authors>
Expand Down
2 changes: 1 addition & 1 deletion N.EntityFrameworkCore.Extensions/Util/CommonUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ internal static string GetJoinConditionSql(Expression<Func<T, T, bool>> joinKeyE
int i = 1;
foreach (var storeGeneratedColumnName in storeGeneratedColumnNames)
{
joinConditionSql += (i > 1 ? "AND" : "") + string.Format("{0}.{2}={1}.{2}", sourceTableName, targetTableName, storeGeneratedColumnName);
joinConditionSql += (i > 1 ? " AND " : "") + string.Format("{0}.{2}={1}.{2}", sourceTableName, targetTableName, storeGeneratedColumnName);
i++;
}
}
Expand Down

0 comments on commit 511a47e

Please sign in to comment.