c# - Error: The underlying provider failed on Open. How to resolve it? -
c# - Error: The underlying provider failed on Open. How to resolve it? -
i have function code similar this:
using(var db = new mydbcontext()) { //a whole bunch of code, db working. //then seek opening dbcontext using(var dba = new anotherdbcontext()) { //about 2 lines of code database } }
and 2 error messages when sec dbcontext:
"the underlying provider failed on open." & "msdtc on server 'myserver' unavailable."
does know reason why happening? can open 2 dbcontexts @ once?
in first scenario, nesting anotherdbcontext. connection database opened each on of them. when phone call service method within using block, new connection opened within mydbcontextwhile there 1 open. causes transaction promoted distributed transaction, , partially committed info (the result of dba.savechanges phone call in service) not beingness available outer connection.also note distributed transactions far slower , thus, has side effect of degrading performance.
private void btntwoconnectionsnested_click(object sender, eventargs e) { string connectionstring = @"data source=" + tbserver.text + @";initial catalog=master;integrated security=true; timeout=0"; using (transactionscope transactionscope = new transactionscope()) { sqlconnection connectionone = new sqlconnection(connectionstring); sqlconnection connectiontwo = new sqlconnection(connectionstring); seek { //2 connections, nested connectionone.open(); connectiontwo.open(); // escalates dtc on 05 , 08 connectiontwo.close(); connectionone.close(); messagebox.show("success"); } grab (exception ex) { messagebox.show("error: " + ex.message); } { connectionone.dispose(); connectiontwo.dispose(); } } }
within 1 transaction scope, open 2 connections, nested. result:
against sql server 2005 instance: msdtc on server ‘servername’ unavailable.
against sql server 2008 instance: msdtc on server ‘servername’ unavailable.
sql server does, , should, escalate dtc nested connection in both versions.
nested connections escalate dtc in sql server 2005 , 2008. opening 1 connection @ time escalate dtc in 2005, not in 2008.
c# sql using
Comments
Post a Comment