asp.net core dbContext isn't for share.

asp.net core dbContext isn't for share.

Feb 14, 2019, 12:00 AM

Entity Framework and DbContext

In short, if inject the DbContext into service and then inject service into controller.

DbContext isn’t designed to support sharing crossing thread.

// Define Local Scope
private readonly AsyncLocal<Scope> scopeProvider = new AsyncLocal<Scope>();
private object RequestScope(IContext context) => scopeProvider.Value;

// Define injection scope with Ninject
kernel.Bind<DbContext>().To<SqlDbContext>().InScope(RequestScope);
kernel.Bind<ISomeDao>().To<SomeDao>().InScope(RequestScope);
kernel.Bind<SomeServiceUseDao>().ToSelf().InScope(RequestScope);