DbContext Disposed after first request when using Ninject's InRequestScope()

前端 未结 3 763
借酒劲吻你
借酒劲吻你 2021-02-10 06:17

I am new to both EF and Ninject so forgive me if this does not make sense :)

I have an MVC3 application with the Ninject and Ninject.Web.Common references. I am trying

3条回答
  •  难免孤独
    2021-02-10 06:32

    Naturally, soon after posting something clicked in my mind, and I was able to solve this.

    The problem lies in the fact that the behavior of ActionFilters were changed in MVC3 and I had a filter that had my ProductService injected.

    I suppose that the filter disposed of the service and that eventually disposed of the DbContext.

    In my case, the solution was easy. I created a second DbContext that is used specifically for my filter. Since the filter does nothing more than query a select few tables to verify authorization to specific resources, I did not need the Unit of Work context that DbContext provides across a single request. I created a new service that uses the new DbContext. In this case, it is sufficient to be configured with InTransientScope()

提交回复
热议问题