Singleton Scope for EF's DbContext

asp.net-mvc c# dependency-injection entity-framework ninject

Question

In order to use Entity Framework in my ASP.NET MVC web application, I'm also utilizing Ninject for Dependency Injection.

In essence, this is how I now register my DbContext and Services with Ninject.

kernel.Bind<DbContext>().To<MyApplicationContext>().InSingletonScope();
kernel.Bind<IAccountService>().To<AccountService>().InSingletonScope();
kernel.Bind<IRegionService>().To<RegionService>().InSingletonScope();
kernel.Bind<IRoleService>().To<RoleService>().InSingletonScope();

I make them members ofInSingletonScope they will only ever be made once and used for the duration of the application (at least how I understand it).

Controllers:

private IAccountService _accountService;

public MemberController(IAccountService accountService)
{
    _accountService = accountService;
}

However, I have a strong suspicion that this singleton scope, because it is a singleton, would cause issues in my online application, particularly for the Entity Framework's context.

Because of this, I already have a small problem: If I manually update the database using SQL Management Studio, the data in my web application's Entity Framework won't update until I restart the application (seems to be some caching mechanism in EF).

--

But if I take away theInSingletonScope , I will occasionally encounter EF errors that state:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

I am aware of why this occurs because the DbContext was initially initialized byAccountService could differ from, say,RegionService But I don't know how I can fix this.

Can somebody offer guidance since I still have very little grasp of Dependency Injection?

--

EDIT: I've attempted switching toInRequestScope for all of the shots, but I'm still receiving

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

in my application when attempting to insert a new entity with a related object (foreign key) from another service. So what's going on if they're still utilizing a distinct DbContext?

LAST EDIT: Okay, I've located the issue. My caching system was caching a previous request, which resulted in the relationship problem on all subsequent requests.

1
5
3/20/2015 9:41:15 AM

Accepted Answer

I've finally figured out how to fix this problem utilizingInRequestScope in place ofInSingletonScope .

I initially continued to experience the same issue after switching toInRequestScope due to the caching system I already have in place on my services layer.

Thus, the reason I was receiving the multiple instances error from EF was because all future queries were using the initially-cached entity object.

--

If you continue to experience the

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

issue following switching toInRequestScope Check to see if any of your entities have been cached or saved for use in future HTTP requests.

1
3/20/2015 9:45:15 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow