using(varTS =NewTransactionScope ()) { stringConnStr ="Data Source=.;i Nitial catalog=test;integrated security=true;"; using(SqlConnection connection =NewSqlConnection (CONNSTR)) { //Create the command and set its properties.SqlCommand Command =NewSqlCommand (); Command. Connection=connection; Command.commandtext="Insertmessage"; Command.commandtype=CommandType.StoredProcedure; Command. Parameters.Add (NewSqlParameter ("@ID",1)); Command. Parameters.Add (NewSqlParameter ("@Message","DFD")); Connection. Open (); Command. ExecuteNonQuery (); Connection. Close (); using(varContext =Newentities ()) {context. Message.add (NewMessage {ID =222, MSG ="DFDFD" }); Context. SaveChanges (); }} ts.complete ();}
Will error unless you open Msdts this Windows service
The reason for this is: If you use a single connection within a TransactionScope, it'll be do as a local transaction (handled within that Connection). If multiple DB Connections is involved, it'll be escalated to a distributed transaction which requires this MSDTC is RU Nning on both the machine where your code runs and on the DB server, and this MSDTC on the client have permissions to Commu Nicate with MSDTC on the server (http://www.datazx.cn/Forums/en-US/0a365486-c57a-4ee9-a59f-e90a4a4abfbf/action?forum =adodotnetentityframework)
EntityFramework5 There's a way to solve this problem.
using (varnew entities ()) { = connstr; Context. Message.add (new222"dfdfd" }); Context. SaveChanges ();}
The following version of the temporary not found a good way, do you have any method?
Discussion on the mixed use of Entity Framework,transactionscope