相关疑难解决方法(0)

如何将TransactionScope与MySql和Entity Framework一起使用?(获得多个同时连接...目前不支持错误)

我有一个新的.NET 4.0控制台应用程序,它使用:

  • MySql 6.4.4.0
  • 实体框架4.2(代码优先)
  • Visual Studio 2010

到目前为止这个工作.我可以很好地添加和读取数据库.

现在,当我添加TransactionScope时,如下例所示:

public static void TestInsert()
{
    using (TransactionScope scope = new TransactionScope())
    {
        using (var context = new MyDbContext())
        {
            // Create a test user
            DateTime dt = DateTime.Now;
            var user1 = new User { UserID = 1, UserName = "test" };
            context.Users.Add(user1);  <-- exception occurs here

            context.SaveChanges();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我收到错误:

目前不支持在同一事务中具有不同连接字符串的多个同时连接或连接.

似乎即使最新版本的MySql也不喜欢使用EntityFramework的TransactionScope.

我希望能够使用事务,尤其是在测试项目中,以便我可以回滚任何更改.

知道如何解决这个问题或解决它吗?

完整的错误信息

System.Data.DataException was unhandled
  Message=An exception occurred while initializing the database. See the InnerException for …
Run Code Online (Sandbox Code Playgroud)

.net mysql transactions entity-framework-4

12
推荐指数
1
解决办法
3256
查看次数

标签 统计

.net ×1

entity-framework-4 ×1

mysql ×1

transactions ×1