Transactionscope与2个不同的数据库

时间:2015-11-13 09:31:37

标签: c# mysql entity-framework transactionscope

我正在尝试为以下方案实施TransactionScope

  • 打开交易范围并使用EF方法执行2 Db插入

  • 获取2

  • 的最后一个INSERT ID
  • 在这里打开一个新的MySQL事务来执行一些本地SQL查询[我在这里得到错误"目前不支持多个同时连接或同一事务中具有不同连接字符串的连接。" ]

如果对不同Db执行了所有查询,则保存TransactionScope否则拒绝

我使用的代码就像

bool _noError = false;
bool can_commit_transaction = false;
bool can_executeSQL = false;

int server_masterId = 0;
int server_sessionId = 0;

// Define a transaction scope for the operations.            
EduAdminEntities transactional_context = new EduAdminEntities();

using (TransactionScope transaction = new TransactionScope())
{
    try
    {
        #region Entity Framework SQL Execution
        schoolmaster master = new schoolmaster { Name = "test name", Address = "address" };                    

        session _session = new session();
        _session.Session = "session 1";
        _session.IsActive = 1;
        master.sessions.Add(_session);

        transactional_context.AddToschoolmasters(master);
        #endregion

        #region Different Db SQL Execution using SQL Command
        //read server last insert ids and if its > 0 execute SQl queries on a different Db using these 2 ids
        server_masterId = master.Id;
        server_sessionId = _session.Id;

        if (server_masterId > 0 && server_sessionId > 0)
        {
            can_executeSQL = true;
        }                  

        if (can_executeSQL)
        {
            MySqlTransaction pageTransaction = null;
            string connection_string = string.Format("Server={0};Port={1};Database={2};User={3};Password={4};", _Server, _Port, _Database, _User, _Password);
            Connection dbConnection = new Connection(connection_string);
            Connection.MyConnection.Open();

            pageTransaction = Connection.MyConnection.BeginTransaction();
            MySqlCommand pageCommand = new MySqlCommand("", Connection.MyConnection, pageTransaction);

            try
            {
                 string _sql = "";
                 #region Schoolmaster configuration
                 pageCommand.Parameters.Clear();
                 _sql = "insert into schoolmaster(Id,Name,Address)values(@Id,@Name,@Address)";
                 pageCommand.CommandText = _sql;
                 pageCommand.Parameters.AddWithValue("@Id", server_masterId);
                 pageCommand.Parameters.AddWithValue("@Name", master.Name);
                 pageCommand.Parameters.AddWithValue("@Address", master.Address);
                 pageCommand.ExecuteNonQuery();

                 pageCommand.Parameters.Clear();
                 _sql = "UPDATE session SET IsActive=0 WHERE IsActive=1 AND  SchoolId= @SchoolId";
                 pageCommand.CommandText = _sql;
                 pageCommand.Parameters.AddWithValue("@SchoolId", server_masterId);

                 pageCommand.ExecuteNonQuery();

                 //-----------------------------
                 pageCommand.Parameters.Clear();
                 _sql = "insert into session(Id,SchoolId,Session,IsActive,Description)values(@Id,@SchoolId,@Session,@IsActive,@Description)";
                 pageCommand.CommandText = _sql;
                 pageCommand.Parameters.AddWithValue("@Id", server_sessionId);
                 pageCommand.Parameters.AddWithValue("@SchoolId", server_masterId);
                 pageCommand.Parameters.AddWithValue("@Session", _session.Session);
                 pageCommand.Parameters.AddWithValue("@IsActive", _session.IsActive);
                 pageCommand.Parameters.AddWithValue("@Description", _session.Description);

                 pageCommand.ExecuteNonQuery();
                 #endregion

                 _noError = true;
             }
             catch (Exception ex)
             {
                 _noError = false;
             }
             finally
             {
                 if (_noError)
                 {
                      pageTransaction.Commit();
                 }
                 else
                 {
                     pageTransaction.Rollback();
                 }

                 if (Connection.MyConnection.State != System.Data.ConnectionState.Closed)
                     Connection.MyConnection.Close();
             }
         }
         #endregion

         transactional_context.SaveChanges(false);
         can_commit_transaction = true;
     }
     catch
     {
     }
}

if (can_commit_transaction)
{   
    transactional_context.AcceptAllChanges();
}

// Dispose the object context.
transactional_context.Dispose();

如何避免错误

  

目前不支持在同一事务中使用不同连接字符串的多个同时连接或连接。

0 个答案:

没有答案