I have an ASP.NET mvc3 application that uses ninject for DI. The application uses Entity Framework 4 and a database initializer, which also puts user data in db.
The database initializer starts at HttpApplication.Application_Start.
However, I get an error because it seems that the child query is being called before the database initializer has finished creating and sowing the db :
The ObjectContext instance has been deleted and can no longer be used for operations that require a connection.
My _layout.cshtml has a child request:
@Html.Action("Present", "Time")
What is it:
[ChildActionOnly]
public ActionResult Present()
{
System.Diagnostics.Debugger.Launch();
Employee employee = this.employeeService.CurrentEmployee;
, "EFZeiterfassungDataContextInitializer" ( DropCreateDatabaseIfModelChanges), .
() !
ninject datacontext ( EFZeiterfassungContext) :
Bind<EFZeiterfassungContext>().ToSelf().InRequestScope();
:
HttpApplication:
public void SetupDependencyInjection()
{
IKernel kernel = new StandardKernel(new ZeiterfassungNinjectModule());
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<IActiveDirectoryService>();
IEmployeeService employeeService = DependencyResolver.Current.GetService<IEmployeeService>();
EFZeiterfassungDataContextInitializer d = new EFZeiterfassungDataContextInitializer(
Server.MapPath("~/Content/UserImages/"),
adService,
employeeService);
Database.SetInitializer<EFZeiterfassungContext>(d);
}
EDIT + :
, Database.SetInitializer(d); . - async. EF , datacontext, db. , var currentEm = employeeService.GetById(1); Database.SetInitializer(d);. DB application_start, .
SetInitializer, .
- , SetInitializer .