Search code examples
c#entity-framework-4navigation-properties

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)


Solution

  • 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();
    }