public static bool DeleteAll()
{
try
{
using (Entities db = new Entities())
{
var temp = (from p in db.CMPS285_table_schedule select p).ToList();
foreach (var item in temp)
{
db.DeleteObject(item);
db.SaveChanges();
}
}
}
catch (Exception E)
{
Console.WriteLine(E);
}
return false;
}
EF is still in the original transaction to retrieve all the data records in Table. So when we call .SaveChanges() to execute the SQL statements in another transaction, we receive the exception.
To workaround the issue, I would recommend you retrieve the results in one go before the foreach loop.
Also please take a look at this link below on deferred and immediate execution.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=750[
^]
Please vote and mark as solution thanks.