An error occurred while starting a transaction on the provider connection. See the inner exception for details

c# entity-framework linq

Question

This is my coding. It shows error like An error occurred while starting a transaction on the provider connection. See the inner exception for details.

       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
5
1/23/2014 11:07:29 AM

Accepted Answer

The db.SaveChanges(); should come outside of the foreach loop:

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();
11
12/18/2015 3:03:41 PM

Popular Answer

Make your Queryable lists to .ToList() it should work fine.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow