You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a generic repository pattern implemented where I set my dbcontext from ambientDbContextLocator. In my service I have using block for dbcontextScopeFactory where repository is used for query.
calling this service function once is fine, but second call says DbContext has been disposed. Tried usign CreateReadOnly as well but no luck. Am I doing something wrong here?
The text was updated successfully, but these errors were encountered:
I'm assuming your _dbContextScopeFactory is injected via an IoC. If your container supports it (AutoFac does) you can inject a Func<IDbContextScopeFactory> dbContextScopeFactoryFunc and change your using statement to the following:
using (var scope = _dbContextScopeFactory().Create()) { ...repo stuff scope.SaveChanges(); }
You can repeat as many using statements in the same code, each will commit separately to the db. Not sure how this behaves for nested ambient repository calls though.
I had same issue right here, when try to call service in second time, the DbContext will throw an exception "The operation cannot be completed because the DbContext has been disposed."
I have a generic repository pattern implemented where I set my dbcontext from ambientDbContextLocator. In my service I have using block for dbcontextScopeFactory where repository is used for query.
using (var dbContextScope = _dbContextScopeFactory.Create())
{
_peopleRepository.Table.Where(x => x.Id == id);
}
calling this service function once is fine, but second call says DbContext has been disposed. Tried usign CreateReadOnly as well but no luck. Am I doing something wrong here?
The text was updated successfully, but these errors were encountered: