using System.Transactions;

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                using (KTStoreContext context = new KTStoreContext())
                        var products = context.Products;
                        foreach (Product product in products)
                            int p = (int)(product.Price * 0.5);
                            product.SPrice = p;
                        int c = context.SaveChanges();
                        KTStoreContext context_s = new KTStoreContext(
                            contextOwnsConnection: false);
                            "UPDATE Product SET SPrice =0 WHERE Id=10 ");
                        Console.WriteLine("更新了 {0} 项数据", c);
                    catch (Exception e) {

netcore 中 使用 TransactionScope


在netcore2.0中使用 TransactionScope是无效的?,有没有人遇到过这种问题?rnorm使用的dapperrn[code=csharp] using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())rn rn using (var conn = SqlConnection())rn rn tryrn rn logger.Trace("事务1:" + System.Transactions.Transaction.Current.TransactionInformation.LocalIdentifier);rn //会员积分账户rn var wallet = Dao.UserScoreWalletDao.Singleton().QueryAccount(transaction.uid, transaction.pool);rn if (!wallet.ok)rn rn return Result.NotOk($"获取会员积分账户错误:wallet.message");rn rn var result = Dao.UserScoreWalletDao.Singleton().ExecuteTransaction(new UserScoreTransaction()rn rn amount = transaction.trans_trade_score,rn ctime = DateTime.Now,rn kind = UserScoreTransaction.Kind_Income,rn notes = $"积分到账[transaction.id]",rn pool = transaction.pool,rn refer = transaction.id.ToString(),rn wid = wallet.value.widrn );rn if (!result.ok || string.IsNullOrEmpty(result.value))rn rn return Result.NotOk($"会员积分到账错误:result.message");rn rn scope.Complete();rn rn catch (Exception ex)rn rn return Result.Exception(ex);rn rn rn [/code]rnrn在 ExecuteTransaction中:rn[code=csharp] public Result ExecuteTransaction(T wat)rn rn tryrn rn using (var scope = new TransactionScope(TransactionScopeOption.Required))rn rn logger.Trace("事务2:" + System.Transactions.Transaction.Current.TransactionInformation.LocalIdentifier);rn var result = ExecuteTransactionIsolate(wat);rn if (result.ok)rn rn scope.Complete();rn rn return result;rn rn rn catch (Exception exp)rn rn return Result.Exception(exp);rn rn rnrn public Result ExecuteTransactionIsolate(T wat)rn rn using (var db = MySqlDB.FromConfiguration(this.DB))rn rn //多表数据操作rn rn rn[/code] 论坛



写了一个最简单的测试代码rn[code=csharp]rn t_provinceDAL dal = new t_provinceDAL();rn using (TransactionScope tran = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 10, 0)))rn rn bool rt = false;rn rt = dal.add("121113");rn rt = dal.delete(11);rn tran.Complete();rn rnrnrn//t_provinceDAL rn public bool add(string pname)rn rn string strSql = "insert into t_province(pname) values(@pname);";rn return DBHelperMySQL.ExecuteNonQuery(strSql, new MySqlParameter("@pname", pname)) > 0;rn rnrn public bool delete(int id)rn rn string strSql = "delete from t_province where id = " + id.ToString();rn return DBHelperMySQL.ExecuteNonQuery(strSql) > 0;rn rnrn//DBHelperrn public static int ExecuteNonQuery(string commandText, params MySqlParameter[] commandParameters)rn rn MySqlConnection connection = new MySqlConnection(connectionString);rn // Create a command and prepare it for executionrn MySqlCommand cmd = new MySqlCommand();rn bool mustCloseConnection = false;rn PrepareCommand(cmd, connection, null, CommandType.Text, commandText, commandParameters, out mustCloseConnection);rnrn // Finally, execute the commandrn int retval = cmd.ExecuteNonQuery();rnrn // Detach the SqlParameters from the command object, so they can be used againrn cmd.Parameters.Clear();rn return retval;rn rnrnrn private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType commandType, string cmdText, MySqlParameter[] cmdParms, out bool mustCloseConnection)rn rn mustCloseConnection = false;rn if (conn.State != ConnectionState.Open)rn conn.Open();rn cmd.Connection = conn;rn cmd.CommandText = cmdText;rn if (trans != null)rn cmd.Transaction = trans;rn cmd.CommandType = CommandType.Text;//cmdType;rn if (cmdParms != null)rn rnrnrn foreach (MySqlParameter parameter in cmdParms)rn rn if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&rn (parameter.Value == null))rn rn parameter.Value = DBNull.Value;rn rn cmd.Parameters.Add(parameter);rn rn rn rnrnrn[/code]rnrn报错rn第一段执行成功了 也没有提交,这是正确的rn到delete就报错了rn提示rnMultiple simultaneous connections or connections with different connection strings inside the same transaction are not currently supported.rn 论坛