Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
       }
like image 202
Golda Avatar asked Jan 23 '14 11:01

Golda


Video Answer


2 Answers

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();
like image 51
Golda Avatar answered Sep 19 '22 09:09

Golda


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

like image 41
zaza Avatar answered Sep 20 '22 09:09

zaza