简体   繁体   English

实体框架导航属性记录从一个记录移动到另一个记录“等待操作超时”

[英]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. 我在Entity框架库中从这个简单的函数中感到头痛。 I've done similar things a million time, but this one just keeps giving me a WIN32Exception the wait operation timed out 我已经做了一百万次类似的事情了,但是这个只是让我一个WIN32Exception等待操作超时

Individu (1 - *) Gifts 个性化(1 - *)礼品

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. 不知何故,我希望我不能将礼物从Individu迁移到另一个,因为我正在循环我正在改变的同一组。

I checked sys.dm_tran_locks and this doesn't show any running locks on either table in sql. 我检查了sys.dm_tran_locks,这没有在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. 有+ - 500.000 INdividus,总共有±1000万礼物。 Each individu has between 0 and 100 gifts 每个人都有0到100个礼物

Do you consider this reason enough to move this code away from EF and run the migrations in an update query? 您是否认为这个原因足以将此代码从EF中移除并在更新查询中运行迁移? (from the above data you can see that i'm only updating 100 rows in one transaction, and that already gives me the timeout) (从上面的数据中你可以看到我只在一个事务中更新了100行,这已经给了我超时)

here are +- 500.000 INdividus, and a total of +- 10 million gifts. 这里有+ - 500.000 INdividus,共有1000万到1000万礼物。 Each individu has between 0 and 100 gifts 每个人都有0到100个礼物

Do you consider this reason enough to move this code away from EF and run the migrations in an update query? 您是否认为这个原因足以将此代码从EF中移除并在更新查询中运行迁移? (from the above data you can see that i'm only updating 100 rows in one transaction, and that already gives me the timeout) (从上面的数据中你可以看到我只在一个事务中更新了100行,这已经给了我超时)

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. 对于原始问题,看起来非常错误,这是在这种情况下直接转到SQL的另一个原因。 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 ? 或者也许在第二个foreach循环中调用SaveChanges?

Jeroen was almost there :) Jeroen几乎在那里:)

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 代码有问题,不要使用.Tolist(),我修改了上面的代码,尝试一下,可能对你有帮助

 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 我在这里考虑个人是列表对象,有另一个列表礼品

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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