Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Which lifestyle to use for a DbContext? #792

srburton opened this issue Jan 3, 2020 · 2 comments

Which lifestyle to use for a DbContext? #792

srburton opened this issue Jan 3, 2020 · 2 comments


Copy link

@srburton srburton commented Jan 3, 2020

I'm having a bad experience with a simple injection, I can't understand why my dbcontext seems to have some kind of cache.

I have my dbcontext registered as "Singleton" and to get back use GetInstance,

I have a GET request that searches the profile information database.

In the first request, everything seems correct. When the second request is made it seems that it returns the same value even if it is changed in the database manually.

Is this some kind of cache?

Is it the type of record I'm using "Singleton" that is causing this behavior?

@srburton srburton added the question label Jan 3, 2020

This comment has been minimized.

Copy link

@srburton srburton commented Jan 3, 2020

I was able to solve using AsNoTracking().

More with my research I saw that it is not indicated to use Singleton in DbContext.

I tried to change to Scoped but I get the following error

SimpleInjector.ActivationException: 'MysqlContext is registered using the' Thread Scoped lifestyle,
but the instance is requested outside the context of an active (Thread Scoped) scope.
Please see for more information on how to apply lifestyles and manage scopes.

This exception was originally thrown at this call stack:

SimpleInjector.Scope.GetScopelessInstance <TImplementation> (SimpleInjector.Lifestyles.ScopedRegistration <TImplementation>)
SimpleInjector.Advanced.Internal.LazyScopedRegistration <TImplementation> .GetInstance (SimpleInjector.Scope)
SimpleInjector.InstanceProducer.GetInstance ()
SimpleInjector.Container.GetInstance <TService> ()
App.Infra.Bootstrap.Ioc.Get <TService> () in Ioc.cs
App.Infra.Data.MysqlContextBase <T> .MysqlContextBase () in MysqlContextBase.cs
App.Application.Authentication.Repositories.AccessRepository.AccessRepository ()
SimpleInjector.InstanceProducer.GetInstance ()

This comment has been minimized.

Copy link

@dotnetjunkie dotnetjunkie commented Jan 3, 2020

Hi @srburton, thank you for choosing Simple Injector.

Before you continue your quest in exploring Simple Injector and Dependency Injection, I can advise reading some of the fine documentation on DI and Simple Injector. I would suggest the following material in this order:

After reading this, you should have an idea about DI in general, and lifestyle management in Simple Injector, and what the caching behaviors are of the different lifestyles that Simple Injector comes with out of the box.

This, however, doesn't directly give you an answer on which lifestyle to use on which component, e.g. your DbContext. This, however, depends on a lot of factors, such as whether:

  • it contains state
  • it is thread safe
  • its data is thread safe
  • it caches data
  • it has dependencies
  • the lifestyles of its dependencies

To understand what lifestyle is best for a DbContext, here are some pointers:

Good luck with your quest. If you have any questions after reading the provided information, feel free to ask.

@dotnetjunkie dotnetjunkie changed the title CACHE, Singleton? DBCONTEX Which lifestyle to use for a DbContext? Jan 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.