How do I use TransactionScope with MySql and Entity Framework? (getting Multiple simultaneous connections…are not currently supported error)

后端 未结 2 706
感情败类
感情败类 2020-12-16 16:20

I have a new .NET 4.0 console application that uses:

  • MySql 6.4.4.0
  • Entity Framework 4.2 (code-first)
  • Visual Studio 2010

So far

相关标签:
2条回答
  • 2020-12-16 16:57

    Try this, Create transaction after creating context.

    public static void TestInsert()
    {
        using (var context = new MyDbContext())
        {
            using (TransactionScope scope = new TransactionScope())
            {
                // Create a test user
                DateTime dt = DateTime.Now;
                var user1 = new User { UserID = 1, UserName = "test" };
                context.Users.Add(user1); 
    
                context.SaveChanges();
    
                scope.Complete();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 17:05

    Kindly complete the scope.

    The Complete method commits the transaction. If an exception has been thrown,

    Complete is not called and the transaction is rolled back.

     scope.Complete();
    

    See MSDN.

    0 讨论(0)
提交回复
热议问题