Entity Framework 6, Transaction Scope, Context and SaveChanges -
question pertains transaction scope , context.savechanges()
. if i'm processing million records foreach
, , save after every, let's say, 1000 records calling context.savechanges()
inside transaction scope , fails after 10 000 have been processed , savechanges()
called, saved data rolled back? example:
using(transactionscope ts = new transactionscope( transactionscopeoption.requiresnew, new timespan(0, 10, 0))) { int counter = 0; using (myentities context = new myentities()) { foreach(var item in context.items) { //process item if(counter >= 1000) { context.savechanges(); //if fail here, saved changes rolled back? counter = 0 } } context.savechanges(); } ts.complete();//what here? }
the transaction scope takes precedence long dbcontext
enrolled in it, default.
thus, if don't call transactionscope.complete
(e.g. because dbcontext
-related exception throws outside of using
block), everything rolled expect. encourage try (best profiler on side monitor what's going on exactly).
in short, every savechanges
hit database, won't commit.
Comments
Post a Comment