Move the deletion of old related info to just before the save

This makes the deletion of BaseItemProviders, BaseItemImageInfos, and BaseItemMetadataFields happen in batch as  a contiguous block so the lock isn't held across items, just before the bulk SaveChanges.
This commit is contained in:
Marc Brooks
2026-08-04 06:14:31 -05:00
parent 812c819162
commit 1c5c95ad1d
2 changed files with 158 additions and 4 deletions
@@ -270,10 +270,6 @@ public class ItemPersistenceService : IItemPersistenceService
}
else
{
context.BaseItemProviders.Where(e => e.ItemId == entity.Id).ExecuteDelete();
context.BaseItemImageInfos.Where(e => e.ItemId == entity.Id).ExecuteDelete();
context.BaseItemMetadataFields.Where(e => e.ItemId == entity.Id).ExecuteDelete();
if (entity.Images is { Count: > 0 })
{
context.BaseItemImageInfos.AddRange(entity.Images);
@@ -403,6 +399,15 @@ public class ItemPersistenceService : IItemPersistenceService
}
}
// Owned rows of updated items are rewritten wholesale; cleared in one statement per table.
if (existingItems.Count > 0)
{
var updatedIds = existingItems.ToArray();
context.BaseItemProviders.WhereOneOrMany(updatedIds, e => e.ItemId).ExecuteDelete();
context.BaseItemImageInfos.WhereOneOrMany(updatedIds, e => e.ItemId).ExecuteDelete();
context.BaseItemMetadataFields.WhereOneOrMany(updatedIds, e => e.ItemId).ExecuteDelete();
}
context.SaveChanges();
var folderIds = tuples