Katta va barqaror to'plamni tezda qanday qilib tozalayman?

Barlar to'plami bilan Foo mavjudligim bor.

Ushbu to'plam juda katta (masalan, 20.000 ta element va ha, u TreeViewda ko'rinadigan bo'lgani uchun butunlay yuklab qo'yilishi kerak) va elementlar avtomatik tarzda Foo ning ba'zi parametrlariga asoslangan holda yaratiladi. foydalanuvchi wpf MVVM dasturini ishlatishi mumkin (barcha ishlov mijozda uchraydi)

U natijalar bilan qoniqqanidan so'ng, u saqlanib qoladi va DbContext.SaveChanges() deyiladi.

Generating Bars implies clearing the collection, then doing a bunch of calculations and adding the new elements. When this is a new, non-persistent Foo, it's no problem because I can just call foo.Bars.Clear()

Muammo shundaki, allaqachon mavjud bo'lgan ob'ektlar bilan EFga barcha elementlarda ularni yo'q qilish o'rniga bar.Foo = null ni o'rnatishiga sabab bo'ladi.

Mening biznesim shu tarzda amalga oshiriladi:

void Regenerate()
{    
    if (fooIsPersistent)
    {
        foreach (var bar in foo.Bars.ToList())
            context.Entry(bar).State = EntityState.Detached;
        deleteOnSave = true;
    }
    else
        foo.Bars.Clear();
    AddTheNewCalculatedElements()
}

void Save()
{
    if (deleteOnSave)
        context.Database.ExecuteSqlCommand("delete Bar where Foo = @Id", param);
    context.SaveChanges();
}

The problem with this solution is that it doesn't scale. Calling Entry(bar).State = EntityState.Detached when there are many entities loaded is even slower than just calling context.Set().Remove(bar) and letting EF do the deletes one by one, so it defeats the purpose of the workaround.

So, my question is: Is there any efficient way to make EF believe the collection is actually empty?

Yoki buni mutlaqo boshqacha qilishim kerakmi?

1

1 javoblar

Xayolimga qaraganda, bu osonroq edi.

Men ushbu satrni almashtirdim:

context.Entry(bar).State = EntityState.Detached;

bu bilan (u ko'p yoki kam ekvivalent deb o'ylardim):

((IObjectContextAdapter)context).ObjectContext.Detach(bar);

Va endi u oqilona vaqtda tugaydi.

1
qo'shib qo'ydi