none
The context cannot be used while the model is being created.

    Question

  • I am noticing this error more and more in my production systems, and I cannot find any way to get around it.  It seems that when EF's CodeFirst context is starting up, and model verification occurs, if another web request happens during that time an InvalidOperationException with an "The context cannot be used while the model is being created".

    This is easily reproducible by going into VS2010 in my MVC project and pressing F5 to enter debug, and while the debug page is loading, if I go into another page that will trigger a DB hit, the InvalidOperationException will occur even before the debug page is loaded.  My context is being loaded via Windsor and has a lifetime of per web request.  It is registered via

    container.Register(Component.For<IUnitOfWork>().ImplementedBy<EFUnitOfWork>().LifeStyle.PerWebRequest);
    

    Does anyone know how to get around this issue?  As my site is not an active site (it's only accessed by a few beta testers at the moment) I have periods of inactivity that seem to cause asp.net to have to completely reload my site.  As this site is storing live data used by these users, I need this error to not occur for them, as it seems to happen in production when they try and reload a site that is starting up for the first time, as they get impatient with Asp.Net loading the site for the first time.  

    Monday, May 23, 2011 11:57 PM

Answers

  • Hey,

    It is a little hard to make suggestions without a repro but there is a brute force approach that should resolve the issue. If you have an interception point before/during DI setup then you can cause all the context initialization etc to happen by creating an instance of your context and calling ctx.Database.Initialize(force: false); Passing 'force: false' will ensure that the initialization still only happens once per AppDomain.

    ~Rowan

    Wednesday, June 01, 2011 5:53 PM
    Moderator

All replies

  • Hi,

    This issue usually arises when you have logic that tries to use the context inside OnModelCreating, any chance you can post up the code from your OnModelCreating?

    ~Rowan

    Tuesday, May 24, 2011 10:49 PM
    Moderator
  • I seem to be having the same issue. Interestingly, I was using Fluent NHibernate before and I was getting errors that were relating to a specific line of code when users are being validated against their roles. I couldn't understand why and after a lot of thought stripped out Fluent to replace it with EF. Now I get the errors at the same line but EF is complaining of an "InvalidOperactionException" with the message same as yours - "The context cannot be used while the model is being created".

    I am using StructureMap for DI and I have a sneaky feeling that it must the DI setup issue.

    Cannot recreate it locally though. I tried what you suggested to recreate it but I get the errors only in Production. I would appreciate if someone could shed some light on this!

    Tuesday, May 31, 2011 7:44 PM
  • Hey,

    It is a little hard to make suggestions without a repro but there is a brute force approach that should resolve the issue. If you have an interception point before/during DI setup then you can cause all the context initialization etc to happen by creating an instance of your context and calling ctx.Database.Initialize(force: false); Passing 'force: false' will ensure that the initialization still only happens once per AppDomain.

    ~Rowan

    Wednesday, June 01, 2011 5:53 PM
    Moderator