我尝试保存更改,因为在会话中运行了其他线程,所以我不允许异常新事务 [英] I try to save changes this I got exception new transaction is not allowed because there are other threads running in the session

查看:67
本文介绍了我尝试保存更改,因为在会话中运行了其他线程,所以我不允许异常新事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试保存更改我得到异常不允许新事务,因为会话中还有其他线程运行

请帮助我这个



我尝试了什么:



I try to save changes this i got exception New transaction is not allowed because there are other threads running in the session
Please help me out from this

What I have tried:

IEnumerable<MTL_TP_RECV_LINES> itemid = ie.MTL_TP_RECV_LINES.Where(x => x.TP_HEADER_ID == headerId).Select(x => x).ToList();
                    //var itemid = ie.MTL_TP_RECV_LINES.Where(x => x.TP_HEADER_ID == headerId).Select(x => x).ToList();
                    foreach (var items in itemid.ToList())
                    {

                        bool stockExists = ie.INV_ITEM_STOCK_LEVELS.Where(x => x.ITEM_ID == items.ITEM_ID && x.OWNED_BY_ID == "5").Any();
                        /* decimal locId, binId, siteId, ownedById, unitPrice, totalCost, qty, lock_tag, status */
                        if (stockExists)
                        {
                            var rcptStock = ie.INV_ITEM_STOCK_LEVELS.Where(x => x.ITEM_ID == items.ITEM_ID && x.OWNED_BY_ID == "5").FirstOrDefault();
                            rcptStock.QTY = rcptStock.QTY + items.QTY;
                            rcptStock.LAST_UPDATE_DATE = DateTime.Now;
                            rcptStock.LAST_UPDATED_BY = (Guid)HttpContext.Current.Session["SiteGUI_ID"];
                           // ie.SaveChanges();
                        }
                        else
                        {
                            string thirdparty="thirdparty";
                            var recplines = ie.MTL_TP_RECV_LINES.Where(x => x.TP_HEADER_ID == headerId && x.ITEM_ID == items.ITEM_ID).FirstOrDefault();

                            ie.INV_ITEM_STOCK_LEVELS.AddObject(new INV_ITEM_STOCK_LEVELS()
                            {
                                ITEM_ID = (decimal)items.ITEM_ID,
                                LOCATION_ID = Convert.ToDecimal(recplines.LOCATION_ID),
                                LOCATION_BIN_ID =Convert.ToDecimal(recplines.BIN_ID),
                                SITE_ID = Convert.ToInt32(HttpContext.Current.Session["Site_ID"]),
                                OWNED_BY_ID = thirdparty,
                                UNIT_PRICE = recplines.UNIT_PRICE,
                                TOTAL_COST = (recplines.QTY) * (recplines.UNIT_PRICE),
                                QTY = recplines.QTY,
                                LOCK_TAG = false,
                                STATUS=0,
                                CREATED_BY = (Guid)HttpContext.Current.Session["SiteGUI_ID"],
                                CREATION_DATE = DateTime.Now,
                                LAST_UPDATE_DATE = DateTime.Now,
                                LAST_UPDATED_BY = (Guid)HttpContext.Current.Session["SiteGUI_ID"]

                            });

                        }

                    }
                    ie.SaveChanges();

推荐答案

你没有显示足够的代码,但看起来你有多个线程试图使用相同的DbContext对象(你的鳕鱼同时叫什么 ie )。这不受支持。每个线程都应该拥有自己的DbContext实例。



还有另一种可能性。您可能有一个foreach遍历由EF返回的集合,但您需要将EF返回的请求转换为List,并在其上调用ToList。这是因为EF在枚举返回的记录时保持与数据库的开放连接。您不能使用与数据库相同的连接来写入它,因为枚举将返回记录。
You dont' show enough code but it looks like you have multiple threads trying to use the same DbContext object (what your cod is calling ie) at the same time. That's not supported. Each thread should have it's own DbContext instance.

There is another possibility. You might have a foreach going over a collection being returned by EF, but you need to convert the request returned by EF to a List be calling ToList on it. This is because EF maintains an open connection to the database as it enumerates the returned records. You cannot use the same connection to the database to write to it as an enumeration is going on returning records.


这篇关于我尝试保存更改,因为在会话中运行了其他线程,所以我不允许异常新事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆