在提供程序连接上启动事务时发生错误。有关详细信息,请参阅内部异常

时间:2014-01-23 11:07:30

标签: c# linq entity-framework

这是我的编码。它显示错误 在提供程序连接上启动事务时发生错误。有关详细信息,请参阅内部异常。

       DemoEntities db = DemoEntities.CreateNewDemoEntity();
       var query = (from f in db.Product_Table
                    where f.ReportID == reportID && f.StateID == stateID
                    select f);
       foreach(var q in query)
       {
           Custom_Search_Transformation cst = new Custom_Search_Transformation()
           {
               CustomerID = customerID,
               StateID = stateID,
               FullProductID = q.FullProductID
           };
           db.Custom_Search_Transformation.AddObject(cst);
           db.SaveChanges();
       }

2 个答案:

答案 0 :(得分:11)

db.SaveChanges();应该超出foreach循环:

DemoEntities db = DemoEntities.CreateNewDemoEntity();
var query = (from f in db.Product_Table
             where f.ReportID == reportID && f.StateID == stateID
             select f);
foreach(var q in query)
{
    Custom_Search_Transformation cst = new Custom_Search_Transformation()
    {
        CustomerID = customerID,
        StateID = stateID,
        FullProductID = q.FullProductID
    };
    db.Custom_Search_Transformation.AddObject(cst);          
}
db.SaveChanges();

答案 1 :(得分:8)

将您的可查询列表设为.ToList()它应该可以正常工作。

相关问题