Skip to content

Commit

Permalink
Merge pull request #1295 from kvpt/softDeleteFix
Browse files Browse the repository at this point in the history
Fix SetSynchronizeSoftDelete option
  • Loading branch information
borisdj authored Oct 18, 2023
2 parents b622a0c + 97b08c5 commit 03934e6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
12 changes: 12 additions & 0 deletions EFCore.BulkExtensions.Tests/EFCoreBulkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,18 @@ private static void RunInsertOrUpdateOrDelete(bool isBulk)
var list = context.Items.Take(2).ToList();
Assert.True(list[0].Quantity != 0);
Assert.True(list[1].Quantity == 0);

// TEST Alias
context.Entries.Add(new Entry { Name = "Entry_InsertOrUpdateOrDelete" });
context.SaveChanges();

int entriesCount = context.Entries.Count();

bulkConfigSoftDel.SetSynchronizeSoftDelete<Entry>(a => new Entry { Name = "Entry_InsertOrUpdateOrDelete_Deleted" });
context.BulkInsertOrUpdateOrDelete(new List<Entry> { new Entry { Name = "Entry_InsertOrUpdateOrDelete_2" } }, bulkConfigSoftDel);

Assert.Equal(entriesCount + 1, context.Entries.Count());
Assert.True(context.Entries.Any(e => e.Name == "Entry_InsertOrUpdateOrDelete_Deleted"));
}

private static void RunUpdate(bool isBulk, SqlType sqlType)
Expand Down
20 changes: 16 additions & 4 deletions EFCore.BulkExtensions.Tests/EFCoreBulkTestAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ private static async Task RunInsertOrUpdateOrDeleteAsync(bool isBulk)
await context.BulkInsertOrUpdateOrDeleteAsync(entities, bulkConfig);
Assert.Equal(0, bulkConfig.StatsInfo?.StatsNumberInserted);
Assert.Equal(EntitiesNumber / 2, bulkConfig.StatsInfo?.StatsNumberUpdated);
Assert.Equal((EntitiesNumber / 2) -1, bulkConfig.StatsInfo?.StatsNumberDeleted);
Assert.Equal((EntitiesNumber / 2) - 1, bulkConfig.StatsInfo?.StatsNumberDeleted);
}
else
{
Expand All @@ -305,11 +305,23 @@ private static async Task RunInsertOrUpdateOrDeleteAsync(bool isBulk)

var bulkConfigSoftDel = new BulkConfig();
bulkConfigSoftDel.SetSynchronizeSoftDelete<Item>(a => new Item { Quantity = 0 }); // Instead of Deleting from DB it updates Quantity to 0 (usual usecase would be: IsDeleted to True)
context.BulkInsertOrUpdateOrDelete(new List<Item> { entities[1] }, bulkConfigSoftDel);
await context.BulkInsertOrUpdateOrDeleteAsync(new List<Item> { entities[1] }, bulkConfigSoftDel);

var list = await context.Items.Take(2).ToListAsync();
Assert.True(list[0].Quantity != 0);
Assert.True(list[1].Quantity == 0);
Assert.True(list[1].Quantity == 0);

// TEST Alias
await context.Entries.AddAsync(new Entry { Name = "Entry_InsertOrUpdateOrDelete" });
await context.SaveChangesAsync();

int entriesCount = await contextRead.Entries.CountAsync();

bulkConfigSoftDel.SetSynchronizeSoftDelete<Entry>(a => new Entry { Name = "Entry_InsertOrUpdateOrDelete_Deleted" });
await context.BulkInsertOrUpdateOrDeleteAsync(new List<Entry> { new Entry { Name = "Entry_InsertOrUpdateOrDelete_2" } }, bulkConfigSoftDel);

Assert.Equal(entriesCount + 1, await contextRead.Entries.CountAsync());
Assert.True(await context.Entries.AnyAsync(e => e.Name == "Entry_InsertOrUpdateOrDelete_Deleted"));
}

private static async Task RunUpdateAsync(bool isBulk, SqlType sqlType)
Expand Down Expand Up @@ -359,7 +371,7 @@ private static async Task RunReadAsync()
entities.Add(new Item { Name = "name " + i });
}

var bulkConfig = new BulkConfig { UpdateByProperties = new List<string> { nameof(Item.Name) }};
var bulkConfig = new BulkConfig { UpdateByProperties = new List<string> { nameof(Item.Name) } };
await context.BulkReadAsync(entities, bulkConfig).ConfigureAwait(false);

Assert.Equal(1, entities[0].ItemId);
Expand Down
4 changes: 3 additions & 1 deletion EFCore.BulkExtensions/SqlAdapters/SqlQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,10 @@ public static (string sql, IEnumerable<object> parameters) MergeTable<T>(DbConte
var querable = context.Set<T>().IgnoreQueryFilters().IgnoreAutoIncludes();
var expression = (Expression<Func<T, T>>)tableInfo.BulkConfig.SynchronizeSoftDelete;
var (sqlOriginal, sqlParameters) = BatchUtil.GetSqlUpdate(querable, context, typeof(T), expression);
var databaseType = SqlAdaptersMapping.GetDatabaseType();
var (tableAlias, _) = SqlAdaptersMapping.GetAdapterDialect().GetBatchSqlReformatTableAliasAndTopStatement(sqlOriginal, databaseType);

var sql = sqlOriginal.Replace("[i]", "T");
var sql = sqlOriginal.Replace($"[{tableAlias}]", "T");
int indexFrom = sql.IndexOf(".") - 1;
int indexTo = sql.IndexOf("\r\n") - indexFrom;
softDeleteAssignment = sql.Substring(indexFrom, indexTo);
Expand Down

0 comments on commit 03934e6

Please sign in to comment.