c# - SetTransactionHandler for ObjectContext -
how can 1 set transactionhandler objectcontext?
i checking example: handling of transaction commit failures, shows dbcontext.
transactionhandler works objectcontext. problem code based configurations (dbconfiguration) not evaluated before first dbcontext instantiated.
two possible workarounds
dummy dbcontext:
public class mydbconfiguration : dbconfiguration { public mydbconfiguration() { settransactionhandler(sqlproviderservices.providerinvariantname, () => new commitfailurehandler()); } } public class testcontext : dbcontext { } static void main(string[] args) { // instantiate dbcontext initialize code based configuration using (var db = new testcontext()) { } using (var db = new transactionhandlerdemoentities()) { var handler = db.transactionhandler; // should commitfailurehandler db.addtodemotable(new demotable { name = "testentiry1" }); db.savechanges(); } }
or dbconfiguration.loaded event
static void main(string[] args) { dbconfiguration.loaded += dbconfiguration_loaded; using (var db = new transactionhandlerdemoentities()) { var handler = db.transactionhandler; db.addtodemotable(new demotable { name = "testentiry1" }); db.savechanges(); } } static void dbconfiguration_loaded(object sender, dbconfigurationloadedeventargs e) { e.adddependencyresolver(new transactionhandlerresolver( () => new commitfailurehandler(), sqlproviderservices.providerinvariantname, null),true); }
transactionhandlerdemoentities objectcontext.
c# entity-framework
No comments:
Post a Comment