0

I'm using TransactionScope to maintain transaction between 2 database. I have created 2 database using 2 different DbContext but I'm getting error on second SaveChanges() method call. I have started MSDTC (Distributed Transaction Coordinator) & dependent service but still getting error.

using (var transaction = new TransactionScope(TransactionScopeOption.Required))
        {
            using (var employeeContext = new EmployeeDbContext())
            {
                var newEmployee = new Employee { Id = 1, Name = "Pankaj" };
                employeeContext.Employee.Add(newEmployee);
                using (var orderContext = new OrderDbContext())
                {
                    var newOrder = new Order { EmployeeId = 1, OrderId = 1, OrdreName = "Test", Amount = 100 };
                    orderContext.Order.Add(newOrder);

                    employeeContext.SaveChanges();
                    orderContext.SaveChanges();//getting error here
                    transaction.Complete();
                }
            }

        }

Error

I'm not getting error When I'm using single dbContext, above error only coming with multiple DbContext

Pankaj Rawat
  • 4,037
  • 6
  • 41
  • 73

1 Answers1

0

Don´t know if you still looking for a solution - but if: Did you´ve tried to change the MS DTC-Settings ? Maybe you don´t "Allow Remote Clients". You can do such setting if you enter dcomcnfg to the search-field (at windows => Start). After that Component Services should open. After that you go to Component Services => Computer => My Computer => Distributed Transaction Coordinator => [Your_DTC]. At the DTC you can open a properties-page where you can change (for example) Security Settings. Checking "Allow Remote Clients" maybe solves yourproblem. If not, try some other Settings..

There are to stackoverflow questions which I´ll recommend:MSSQL Error 'The underlying provider failed on Open' and How to run two Entity Framework Contexts inside TransactionScope without MSDTC?

You´ve said:

I'm not getting error When I'm using single dbContext, above error only coming with multiple DbContext

The reason for this behaviour is pretty clear: TransactionScope helps you handling transactions. Microsoft provides per default two Transaction-Managers: LTM (lightweight transaction manager) for "easy" transactions and DTC for distributed transactions. MS automatically escalates transactions to DTC, if the transactions meets some criteria. One criteria is a transactional access on two different databases. So if you want to "workaround" your problem, and if this is possible, you can try to avoid multiple database access in one transaction. As a result of this modification your transaction will not be escalated to MS DTC anymore.

Community
  • 1
  • 1
Joshit
  • 1,238
  • 16
  • 38
  • Here is one more stackoverflow question which may help you http://stackoverflow.com/questions/11451112/why-is-my-transactionscope-trying-to-use-msdtc-when-used-in-an-ef-code-first-app?rq=1 – Joshit Feb 09 '17 at 10:34
  • Perfect, work for me. 1st I followed above step and run MSDTC & dependent server using "Network Service" account. now my transaction working properly :) Thanks alot @Joshit – Pankaj Rawat Feb 10 '17 at 03:14