将Foreach转换为Parallel时出现EF错误

时间:2018-08-08 15:54:48

标签: c#

我只是想将Foreach循环转换为Parallel,但问题是当我将其转换为Parallel时出现异常。异常表示类似这样的内容-InvalidOperationException: The connection was not closed. The connection's current state is connecting.因此,并行使用的entityframework出现了问题。我还附有错误图片。任何人都可以告诉我对此可能的解决方案是什么? Foreach处理我的任务花费的时间太长,这就是为什么要使用Parallel。那么解决方案是什么?

pic

Foreach代码:

foreach (var _rez in rez)
                        {
                            if (_rez.UPC != null && Regex.IsMatch(_rez.UPC, @"^\d+$") == true)
                            {
                                if (ctx.ThirdPartyData.Any(x => x.UPC == _rez.UPC) == true)
                                {
                                    int type = ctx.ThirdPartyData.Where(x => x.UPC == _rez.UPC).FirstOrDefault().Type.Value;
                                    if (UserPre.SellerSite == ".com" && type == 1)
                                    {
                                        _rez.IsGreenMarked = true;
                                    }
                                    if (UserPre.SellerSite == ".co.uk" && type == 2)
                                    {
                                        _rez.IsGreenMarked = true;
                                    }
                                    if (UserPre.SellerSite == ".de" && type == 3)
                                    {
                                        _rez.IsGreenMarked = true;
                                    }
                                }

                            }
                            if (_rez.EAN != null && Regex.IsMatch(_rez.EAN, @"^\d+$") == true)
                            {
                                if (ctx.ThirdPartyData.Any(x => x.EAN == _rez.EAN) == true)
                                {
                                    int type = ctx.ThirdPartyData.Where(x => x.EAN == _rez.EAN).FirstOrDefault().Type.Value;
                                    if (UserPre.SellerSite == ".com" && type == 1)
                                    {
                                        _rez.IsGreenMarked = true;
                                    }
                                    if (UserPre.SellerSite == ".co.uk" && type == 2)
                                    {
                                        _rez.IsGreenMarked = true;
                                    }
                                    if (UserPre.SellerSite == ".de" && type == 3)
                                    {
                                        _rez.IsGreenMarked = true;
                                    }
                                }

                            }
                        }

并行代码:

                var UserPre = ctx.UserPreferences.FirstOrDefault(x => x.UserId == uid);
                Parallel.ForEach(rez, _rez =>
                {
                    if (_rez.UPC != null && Regex.IsMatch(_rez.UPC, @"^\d+$") == true)
                    {
                        if (ctx.ThirdPartyData.Any(x => x.UPC == _rez.UPC) == true)
                        {
                            int type = ctx.ThirdPartyData.Where(x => x.UPC == _rez.UPC).FirstOrDefault().Type.Value;
                            if (UserPre.SellerSite == ".com" && type == 1)
                            {
                                _rez.IsGreenMarked = true;
                            }
                            if (UserPre.SellerSite == ".co.uk" && type == 2)
                            {
                                _rez.IsGreenMarked = true;
                            }
                            if (UserPre.SellerSite == ".de" && type == 3)
                            {
                                _rez.IsGreenMarked = true;
                            }
                        }

                    }
                    if (_rez.EAN != null && Regex.IsMatch(_rez.EAN, @"^\d+$") == true)
                    {
                        if (ctx.ThirdPartyData.Any(x => x.EAN == _rez.EAN) == true)
                        {
                            int type = ctx.ThirdPartyData.Where(x => x.EAN == _rez.EAN).FirstOrDefault().Type.Value;
                            if (UserPre.SellerSite == ".com" && type == 1)
                            {
                                _rez.IsGreenMarked = true;
                            }
                            if (UserPre.SellerSite == ".co.uk" && type == 2)
                            {
                                _rez.IsGreenMarked = true;
                            }
                            if (UserPre.SellerSite == ".de" && type == 3)
                            {
                                _rez.IsGreenMarked = true;
                            }
                        }

                    }
                });

0 个答案:

没有答案