Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 3 child request kicks in before Entity Framework DatabaseInitializer finished creating tables and seeding the db?
    text
    copied!<p>I have an ASP.NET mvc3 application which is using ninject for DI. The app is making use of Entity Framework 4 and a database initializer that also seeds custom data into the db.</p> <p>The database initializer runs in HttpApplication.Application_Start.</p> <p><strong>However I get an error because it seems that the child request is called before the database initializer finished creating and seeding the db</strong>:</p> <blockquote> <p>The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.</p> </blockquote> <p>My _layout.cshtml has some child request:</p> <pre><code>@Html.Action("Present", "Time") </code></pre> <p>That it:</p> <pre><code>[ChildActionOnly] public ActionResult Present() { //HttpContext.Trace.Write("Inside [ChildActionOnly]\npublic ActionResult Present() (child requests that is called by _layout.cshtml)"); System.Diagnostics.Debugger.Launch(); Employee employee = this.employeeService.CurrentEmployee; </code></pre> <p>However once the database is successfully created and seeded with my database initializer called "EFZeiterfassungDataContextInitializer" (it inherits from DropCreateDatabaseIfModelChanges) the app is working. </p> <p>I'm only having this problem with the first (child) request!</p> <p>In my ninject module I configured my datacontext (called EFZeiterfassungContext) like this:</p> <pre><code>Bind&lt;EFZeiterfassungContext&gt;().ToSelf().InRequestScope(); </code></pre> <p>Some more info:</p> <p>Here is some code of my HttpApplication:</p> <pre><code>public void SetupDependencyInjection() { // Create Ninject DI Kernel IKernel kernel = new StandardKernel(new ZeiterfassungNinjectModule()); //Tell asp.net mvc to use our ninject di container DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel)); } protected void Application_Start() { SetupDependencyInjection(); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); InitializeDatabase(); } private void InitializeDatabase() { IActiveDirectoryService adService = DependencyResolver.Current.GetService&lt;IActiveDirectoryService&gt;(); IEmployeeService employeeService = DependencyResolver.Current.GetService&lt;IEmployeeService&gt;(); EFZeiterfassungDataContextInitializer d = new EFZeiterfassungDataContextInitializer( Server.MapPath("~/Content/UserImages/"), adService, employeeService); Database.SetInitializer&lt;EFZeiterfassungContext&gt;(d); } </code></pre> <p>EDIT + workaround:</p> <p>Obviously <code>Database.SetInitializer(d);</code> returns immediately. the initializer isn't run immediately - not even async. EF first checks the DB for changes if you have some interaction with the datacontext i.e. by querying some value from the not yet existing db. I could workaround it by calling <code>var currentEm = employeeService.GetById(1);</code> after <code>Database.SetInitializer(d);</code>. so the DB really gets initialized on application_start and not later.</p> <p>I would like to see some overloaded method of <code>SetInitializer</code> so you can immediately trigger that process.</p> <p>I'm still interested in a better solution for this until <code>SetInitializer</code> has such an overloaded method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload