简体   繁体   中英

Entity Framework Navigational property record moving from one record to another “Wait operation timed out”

I'm getting headaches from this simple function in my Entity framework repository. I've done similar things a million time, but this one just keeps giving me a WIN32Exception the wait operation timed out

Individu (1 - *) Gifts

Somehow i expect that I can't migrate the gifts from Individu to another because i'm looping over the same set that i'm altering.

I checked sys.dm_tran_locks and this doesn't show any running locks on either table in sql.

   public void MigrateDataForInd(Individu from, Individu to){
        foreach (var item in from.Gifts.ToList()) {
            to.Gifts.Add(item);
            //also tried: item.Individu = to;
        }
        this.SaveChanges();
     }

Any ideas on how to circumvent this issue? (increasing the lock time out period doesn't help, and i'm getting the same error when using other navigational properties (gifts is just one of them)

UPDATE: To give you guys an image of the amount of data i'm dealing with here:

There are +- 500.000 INdividus, and a total of +- 10 million gifts. Each individu has between 0 and 100 gifts

Do you consider this reason enough to move this code away from EF and run the migrations in an update query? (from the above data you can see that i'm only updating 100 rows in one transaction, and that already gives me the timeout)

here are +- 500.000 INdividus, and a total of +- 10 million gifts. Each individu has between 0 and 100 gifts

Do you consider this reason enough to move this code away from EF and run the migrations in an update query? (from the above data you can see that i'm only updating 100 rows in one transaction, and that already gives me the timeout)

Absolutely! You'd need to have a very compelling reason to get all that data out of the database to be later sent back. The difference in performance is huge.

As for the original question, something looks very wrong, which is another reason to go directly to SQL in this case. If you run into trouble with the updates, look into the execution plan and it may get you closer.

What if you try this:

 public void MigrateDataForInd(Individu from, Individu to){
        var offlineGifts = from.Gifts.ToList();
        foreach (var offlineGift in offlineGifts ) {
            from.Gifts.DeleteObject(offlineGift );
            to.Gifts.Add(offlineGift );
        }
        this.SaveChanges();
     }

Or maybe call SaveChanges in the second foreach loop ?

Jeroen was almost there :)

It should be remove.

public void MigrateDataForInd(Individu from, Individu to)
{
    var offlineGifts = from.Gifts.ToList();
    foreach (var item in offlineGifts)
    {
        from.Gifts.Remove(item);
        to.Gifts.Add(item);
    }
    this.Save();
}

Incresing time out time will not resolve this,

there is problem with code, Dont use .Tolist(), I have modified above code, try it, may it help you

 public void MigrateDataForInd(Individu from.Gifts, Individu1 to){
        foreach (var item in from.Gifts) {
            to.Gifts.Add(item);            
        }
        this.SaveChanges();
     }

I am considering here Individu is list object which having another list Gift

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM