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();
}
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();
Make your Queryable lists to .ToList() it should work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With