c# 4.0 - Multiple databases(datacontext) on same server without MS DTC -


i'm using ef5.0 sql server 2008. have 2 databases on same server instance. need update tables on both databases , want them same transaction. used transactionscope. below code -

public void save() {         var msobjectcontext = ((iobjectcontextadapter)msdatacontext).objectcontext;         var awobjectcontext = ((iobjectcontextadapter)awcontext).objectcontext;          using (var scope = new transactionscope(transactionscopeoption.required,                                              new transactionoptions                                                  {                                                      isolationlevel = isolationlevel.readuncommitted                                                  }))         {                            msobjectcontext.savechanges(saveoptions.detectchangesbeforesave);             awobjectcontext.savechanges(saveoptions.detectchangesbeforesave);              scope.complete();         }     } 

when use above code transaction gets promoted dtc. after searching on internet found happens because of 2 different connectionstrings / connections. dont understand if write stored procedure on 1 database updates table in different database (on same server) no dtc required. why ef or transactionscope promoting dtc? there other work around this?

please advise

thanks in advance

sai

with plain dbconnections, can prevent dtc escalation multiple databases on same server using same connection string (with database like) , manually change database on opened connection object so:

using (var tx = new transactionscope()) {     using (var conn = new sqlconnection(connectstr))     {         conn.open();         new sqlcommand("insert atest values (1)", conn).executenonquery();     }     using (var conn = new sqlconnection(connectstr))     {         conn.open();         conn.changedatabase("otherdb");         new sqlcommand("insert btest values (2)", conn).executenonquery();     }     tx.complete(); } 

this not escalate dtc, would, if used different values connectstr.

i'm not familiar ef , how manages connections , contexts, using above insight, might able avoid dtc escalation doing conn.changedatabase(..) , creating context new dbcontext(conn, ...).

but please note shared connect string, have 2 connections open @ same time, dtc involved, in modified example:

using (var tx = new transactionscope()) {     using (var conn = new sqlconnection(mssqldb))     {         conn.open();         new sqlcommand("insert atest values (1)", conn).executenonquery();         using (var conn2 = new sqlconnection(mssqldb))         {             conn2.open();             conn2.changedatabase("otherdatabase");             new sqlcommand("insert btest values (2)", conn2).executenonquery();         }     }     tx.complete(); } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -