我试图将数据从ProductStatisticsTemp
表逐行复制到ProductStatistics
表。
[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)
。一旦我调试,我可以看到以下内部异常消息
消息=&#34;不允许新事务,因为还有其他事务 在会话中运行的线程。&#34;
答案 0 :(得分:0)
尝试使用:
IList<ProductStatistics> str = from a db.ProductStatisticsTemp select a;
foreach (var val in str)
{
// your code
}
您需要调用EF并尝试将其返回到该目标类型的IList<T>
,然后我们可以在IList<T>
上使用循环
您还可以查看相关的thread