Fix stack overflow exception when updating large databases (#1158)
This commit is contained in:
parent
211f15af25
commit
6c5773df24
@ -369,11 +369,11 @@ namespace ApplicationServices
|
|||||||
|
|
||||||
using var context = DbContexts.GetContext();
|
using var context = DbContexts.GetContext();
|
||||||
|
|
||||||
// Attach() NoTracking entities before SaveChanges()
|
// Entry() NoTracking entities before SaveChanges()
|
||||||
foreach (var lb in removeLibraryBooks)
|
foreach (var lb in removeLibraryBooks)
|
||||||
{
|
{
|
||||||
lb.IsDeleted = true;
|
lb.IsDeleted = true;
|
||||||
context.Attach(lb).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
context.Entry(lb).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
var qtyChanges = context.SaveChanges();
|
var qtyChanges = context.SaveChanges();
|
||||||
@ -398,11 +398,11 @@ namespace ApplicationServices
|
|||||||
|
|
||||||
using var context = DbContexts.GetContext();
|
using var context = DbContexts.GetContext();
|
||||||
|
|
||||||
// Attach() NoTracking entities before SaveChanges()
|
// Entry() NoTracking entities before SaveChanges()
|
||||||
foreach (var lb in libraryBooks)
|
foreach (var lb in libraryBooks)
|
||||||
{
|
{
|
||||||
lb.IsDeleted = false;
|
lb.IsDeleted = false;
|
||||||
context.Attach(lb).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
context.Entry(lb).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
var qtyChanges = context.SaveChanges();
|
var qtyChanges = context.SaveChanges();
|
||||||
@ -518,16 +518,17 @@ namespace ApplicationServices
|
|||||||
if (libraryBooks is null || !libraryBooks.Any())
|
if (libraryBooks is null || !libraryBooks.Any())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
foreach (var book in libraryBooks)
|
|
||||||
action?.Invoke(book.Book.UserDefinedItem);
|
|
||||||
|
|
||||||
using var context = DbContexts.GetContext();
|
using var context = DbContexts.GetContext();
|
||||||
|
|
||||||
// Attach() NoTracking entities before SaveChanges()
|
// Entry() instead of Attach() due to possible stack overflow with large tables
|
||||||
foreach (var book in libraryBooks)
|
foreach (var book in libraryBooks)
|
||||||
{
|
{
|
||||||
context.Attach(book.Book.UserDefinedItem).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
action?.Invoke(book.Book.UserDefinedItem);
|
||||||
context.Attach(book.Book.UserDefinedItem.Rating).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
|
||||||
|
var udiEntity = context.Entry(book.Book.UserDefinedItem);
|
||||||
|
|
||||||
|
udiEntity.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
||||||
|
udiEntity.Reference(udi => udi.Rating).TargetEntry.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
var qtyChanges = context.SaveChanges();
|
var qtyChanges = context.SaveChanges();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user