mardi 15 avril 2014

Entity Framework - force commit without transaction


Vote count:

0




I have a loop that looks a bit like this:



CountsToReport = rep.Counts_Get().Where(x => x.Status == "Completed");

foreach (var count in CountsToReport.ToList())
{
//do some stuff
//send an email

count.Status = "Reported";
rep.SaveChanges();
}


Where "rep" is a repository wrapper around an EF context.


When this runs, the unfortunate email recipient gets deluged with spam because the SaveChanges call doesn't actually commit the changes - so the loop keep getting the same counts, emailing them, and marking them as "Reported" but doesn't actually save the change.


If you stop the loop, and re-start the code, the change saves successfully. You can confirm this scenario by stepping through the code: the EF object in C# changes its Status, but the underlying data in SQL doesn't change.


I'm presuming this is because SaveChanges doesn't actually commit the transaction - it just marks the data as having changed ready for the end of the transaction. But we're not using transactions anywhere else in the DB, and it'd be a bit of a pain to change the repository for this one use case.


Is there any other way I can force EF to commit this change and escape my endless loop of doom? Or am I mistaken about the cause?



asked 47 secs ago






Aucun commentaire:

Enregistrer un commentaire