question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple DbContexts with single database - Is MSDTC required ?

See original GitHub issue

Let’s say we have multiple DbContexts, but all tables are in a single database.

If we use multiple DbContexts inside the same UOW like this ABP test:

namespace Abp.TestBase.SampleApplication.Tests.People
{
    public class Two_DbContext_Uow_Tests : SampleApplicationTestBase
    {
        private readonly IRepository<Person> _personRepository;
        private readonly IRepository<SecondDbContextEntity> _secondDbContextEntityRepository;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        public Two_DbContext_Uow_Tests()
        {
            _personRepository = Resolve<IRepository<Person>>();
            _secondDbContextEntityRepository = Resolve<IRepository<SecondDbContextEntity>>();
            _unitOfWorkManager = Resolve<IUnitOfWorkManager>();
        }

        //[Fact] //TODO: Not working since Effort does not support multiple db context!
        public async Task Should_Two_DbContext_Share_Same_Transaction()
        {
            var personInitial = UsingDbContext(context => context.People.Single(p => p.Name == "halil"));

            using (var uow = _unitOfWorkManager.Begin())
            {
                await _personRepository.UpdateAsync(new Person
                {
                    Id = personInitial.Id,
                    Name = "halil-updated",
                    ContactListId = personInitial.ContactListId
                });

                await _secondDbContextEntityRepository.InsertAsync(new SecondDbContextEntity {Name = "test1"});

                await uow.CompleteAsync();
            }

            var personFinal = UsingDbContext(context => context.People.Single(p => p.Id == personInitial.Id));
            personFinal.Name.ShouldBe("halil-updated");

            using (var secondContext = Resolve<SecondDbContext>())
            {
                secondContext.DisableAllFilters();
                secondContext.SecondDbContextEntities.Count(e => e.Name == "test1").ShouldBe(1);
            }
        }
    }
}

Would that require MSDTC ? Or since it is the same database, all DbContext will share the same connection/transaction ?

Thanks !

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
guilmoricommented, Jan 18, 2017

For the records, I also tested with the default TransactionScopeEfTransactionStrategy, on Azure Web App, injecting 2 repositories using different DbContexts in the same UOW, and doing Query/Update on both. To my surprise, this is also working. I guess the TransactionScope is smart enough to detect the connection string is the same.

0reactions
hikalkancommented, Aug 10, 2017

Can you create another Github issue and write steps to reproduce the problem with your exception message, stack trace and sample code?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple DbContext instances to the same database using ...
If two DbContext instances try to share a System.Transactions.Transaction at the same time a distributed transaction is required.
Read more >
Intermittent Behavior With Multiple DbContexts in MSDTC ...
For now, my plan is to adjust the tests so that they only use one DbContext ... class Context : DbContext { static...
Read more >
C#/NET/EF Core: I have multiple DBContexts. How do I ...
I have multiple dbcontexts; one for each database connection. The databases are on separate servers. I need to do a bunch of CRUD...
Read more >
Multiple Database #4350
Hi,. It require MSDTC. Azure doesn't support MSDTC. can you provide other solution to use multiple database and multiple dbcontet using this ...
Read more >
Working with Transactions - EF6
Starting with EF6 Database.ExecuteSqlCommand() by default will wrap the command in a transaction if one was not already present.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found