Using DbContext in nested scope
See original GitHub issueHi.
I created class marked with [UnitOfWork(isTransactional: true)]
attribute.
Then i injected IRepository<Entity>
into ctor.
In method i create something like this:
var xx = repTest.GetAll().Take(1).ToList();
using (var uov = uowManager.Begin(new UnitOfWorkOptions()
{
Scope = System.Transactions.TransactionScopeOption.Suppress,
IsTransactional = false,
}))
{
var xx = repTest.GetAll().Take(1).ToList();
uov.Complete();
}
If i look into SQL profiler first request is create inside transaction (SPID: x) and second outside of transaction (SPID: y). So everything work perfect and as expected.
If i replace repository with dbcontext then second query is executed inside of transaction (same SPID).
Why is that ?
I have injected IDbContextProvider<TestDbContext>
into ctor and then get context from provider
(calling .GetDbContext method).
Please explain why ABP works like that. Is this bug ?
Thanks for advance.
Edvin
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Is there a concept of scope or nested contexts within EF ...
Let's say my application's DbContest is scoped to the http request. The way I would do it normally is any method that needs...
Read more >Managing DbContext the right way with Entity Framework 6
Once a business transaction has completed and has called the DbContext.SaveChanges() method to persist all the changes it made, it's entirely ...
Read more >Make the DbContext Ambient with UnitOfWorkScope
I've been looking for a better way to handle the DbContext and have come up with an ambient DbContext, using a UnitOfWorkScope which...
Read more >Using Transactions - EF Core
You can use the DbContext.Database API to begin, commit, and rollback transactions. The following example shows two SaveChanges operations ...
Read more >Entity Framework Core - Use TransactionScope With ...
In this article: Async methods. BeginTransaction within TransactionScope. Multiple instances of DbContext (or rather DB connections).
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
From the MSDN article on The Repository Pattern:
So yes, we generally avoid using
DbContext
directly.The limitation is that
IRepository
only has methods that use a subset ofDbContext
methods. In other cases, usingDbContext
may be necessary. That’s where Custom Repositories come in.You are using the context from the outer unit of work. Try this: