Entity Framework Context in Singleton

21,681

NHibernate doesn't use Session as singleton ... Such scenario has only meaning in very rare cases where your application is very short batch representing single transaction / unit of work.

Here are described reasons why you should not use shared / long living context. In case of multithreaded or server application serving multiple clients you must not use shared context.

Share:
21,681
Acaz Souza
Author by

Acaz Souza

@acazsouza

Updated on July 09, 2022

Comments

  • Acaz Souza
    Acaz Souza almost 2 years

    I'm building a App that use Context of EF in Singleton Pattern like NHibernate work with Session:

    public class DbContextFactory
    {
        private static volatile DbContextFactory _dbContextFactory;
        private static readonly object SyncRoot = new Object();
        public DbContext Context;
    
        public static DbContextFactory Instance
        {
            get
            {
                if (_dbContextFactory == null)
                {
                    lock (SyncRoot)
                    {
                        if (_dbContextFactory == null)
                            _dbContextFactory = new DbContextFactory();
                    }
                }
                return _dbContextFactory;
            }
        }
    
        public DbContext GetOrCreateContext()
        {
            if (this.Context == null)
                this.Context = new DbContext(ConfigurationManager.AppSettings["DefaultConnectionString"]);
    
            return Context;
        }
    }
    

    I'm using Ninject to Inject the Context:

    public class DbContextModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IDbContext>().ToConstant(DbContextFactory.Instance.GetOrCreateContext());
        }
    }
    

    I'm reading about that approach and some people are saying that is bad practice and I'll have problems.

    Someone that know about this with EF can explain me in more details?

    • Gert Arnold
      Gert Arnold almost 10 years
      Did you only read it's bad practice? Without explanation?
  • Acaz Souza
    Acaz Souza over 12 years
    What's Singleton in NHibernate? The SessionFactory?
  • Acaz Souza
    Acaz Souza over 12 years
    Look at my approach, exist any problem? I can say to Ninject use in a RequestScope.
  • Ladislav Mrnka
    Ladislav Mrnka over 12 years
    Yes the factory can be used as a singleton and create a new Session each time it is required. Request scope is normally used for context in web application scenarios.
  • Acaz Souza
    Acaz Souza over 12 years
    I'm confusing, look at my approach, I'm resolving the problem put InRequestScope in Ninject Configuration?
  • Ladislav Mrnka
    Ladislav Mrnka over 12 years
    I don't see any request scope in your code and still using your singleton factory in request scope will not solve it because it will return the same context instance every time.
  • Acaz Souza
    Acaz Souza over 12 years
    please, you can fix my code using a context in request scope? Or have some code of example? Thanks, I'll very grateful;
  • Ladislav Mrnka
    Ladislav Mrnka over 12 years
    You must not use ToConstant. You must use To like: Bind<IDbContext>().To<YourDerivedContext>().InRequestScope()
  • Onots
    Onots almost 10 years
    Please add the relevant parts of the article to your answer. That way if the link fails your answer will still make sense.
  • Pétur Ingi Egilsson
    Pétur Ingi Egilsson about 7 years
    Article no longer available.