Multiple DbContexts with single database - Is MSDTC required ?
See original GitHub issueLet’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:
- Created 7 years ago
- Comments:20 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Can you create another Github issue and write steps to reproduce the problem with your exception message, stack trace and sample code?