【问题标题】:Message = "New transaction is not allowed because there are other threads running in the session." [duplicate]Message = "不允许新事务,因为会话中正在运行其他线程。" [复制]
【发布时间】:2016-07-07 13:26:00
【问题描述】:

我正在尝试将数据从ProductStatisticsTemptable 逐行复制到ProductStatistics table。

    [HttpGet]
    public ActionResult Copy_DatatoProductStatistics()
    {
        string errormsg = "";
        //var incomplete_product_result = db.Database.SqlQuery<ProductStatistics>("Copy_ProductStatistics");
        var str = from a in db.ProductStatisticsTemp select a;

        ProductStatistics ls = new ProductStatistics();

        try
        {
            foreach (var val in str) // iterator the data from the list and insert them into the listSecond
            {
                var product = db.Product.Find(val.Product_ID);

                try
                {

                    if (product.ProductID == val.Product_ID & product.ProductTitleEn == val.ProductNameEn & product.ProductTitleAr == val.ProductNameAr)
                    {
                        try
                        {

                            if (!String.IsNullOrEmpty(val.SalesVolumes) | !String.IsNullOrEmpty(val.Target) | !String.IsNullOrEmpty(val.Profitability) | !String.IsNullOrEmpty(val.AnnualGrowthRate))
                            {
                                ls.Product_ID = val.Product_ID;
                                ls.ProductNameEn = val.ProductNameEn;
                                ls.ProductNameAr = val.ProductNameAr;
                                ls.Profitability = val.Profitability;
                                ls.Target = val.Target;
                                ls.SalesVolumes = val.SalesVolumes;
                                ls.Subsidary_ID = val.Subsidary_ID;
                                ls.AnnualGrowthRate = val.AnnualGrowthRate;

                                db.ProductStatistics.Add(ls);

                            }

                               db.SaveChanges();

                        }

                        catch (Exception ex)
                        {
                            errormsg = ex.Message;
                        }


                    }

                }
                catch (Exception ex)
                {
                    errormsg = "Product ID or Product Name Doesnt match with existing details";
                }

            }           


        }

        catch (Exception ex)
        {
            errormsg = ex.Message;                
        }


        return RedirectToAction("FileUpload", "FileUpload", new { error = errormsg });

    }

但在上面的db.SaveChanges(); 之后,它指向catch (Exception ex)。调试后,我可以看到以下内部异常消息

Message = "不允许新交易,因为还有其他 会话中运行的线程。”

【问题讨论】:

  • 永远不要从控制器代码调用你的数据库代码...创建一个你应该从控制器调用的数据访问层
  • 如果你用谷歌搜索该错误信息并阅读前几条点击,你就会明白如何解决它。

标签: c# asp.net-mvc multithreading linq asp.net-mvc-5


【解决方案1】:

尝试使用:

IList<ProductStatistics> str = from a db.ProductStatisticsTemp select a;
foreach (var val in str)
{

    // your code
}

您需要调用 EF 并尝试将其返回到该目标类型的 IList&lt;T&gt;,然后我们可以在 IList&lt;T&gt; 上使用循环

你也可以查看这个相关的thread

【讨论】:

  • 在一般情况下我们不需要在 EF 查询末尾使用 .ToList() 吗?
  • @teovankot:- 是的,我们需要。
  • Cannot implicitly convert type 'System.Linq.IQueryable&lt;project_name.Models.ProductStatisticsTemp&gt;' to 'System.Collections.Generic.IList&lt;project_name.Models.ProductStatisticsTemp&gt;'. 收到此错误
猜你喜欢
  • 1970-01-01
  • 2012-09-07
  • 2017-03-27
  • 2012-04-23
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 2017-06-26
相关资源
最近更新 更多