的DbContext设置的第一请求后,在使用Ninject的InRequestScope() [英] DbContext Disposed after first request when using Ninject's InRequestScope()

查看:216
本文介绍了的DbContext设置的第一请求后,在使用Ninject的InRequestScope()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的都EF和Ninject所以请原谅我,如果这是没有意义的:)

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

我与Ninject和Ninject.Web.Common引用一个MVC3应用程序。我想注入的DbContext进入我的仓库。我所看到的是,在第一次请求,一切正常,但奇妙的后续请求返回:

I have an MVC3 application with the Ninject and Ninject.Web.Common references. I am trying to inject a DbContext into my repositories. What I am seeing is that on the first request, everything works wonderfully but the subsequent requests return:

System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)

我的绑定:

kernel.Bind<ISiteDataContext>().To<SiteDataContext>().InRequestScope();
kernel.Bind<IProductRepository>().To<ProductRepository>();
kernel.Bind<IProductService>().To<ProductService>();

我的服务类:

public class ProductService : IProductService {
    [Inject]
    public IProductRepository repository {get; set;}

    ...
}

我的仓库类:

public class ProductRepository : IProductRepository {
    [Inject]
    public ISiteDataContext context {get; set;}

    ...
}

我SiteDataContext类:

My SiteDataContext class:

public class SiteDataContext  : DbContext, ISiteDataContext 
{
    static SiteDataContext()
    {
        Database.SetInitializer<SiteDataContext >(null);
    }

    public DbSet<Product> Products{ get; set; }


    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
    }
}

我的控制器:

public class ProductController {
    [Inject]
    public IProductService productService {get; set;}

    ...
}

如果我删除.InRequestScope(),然后它工作正常 - 但随后,导致实体框架的问题,因为对象是在数据上下文的多个单独的实例修改。

If I remove .InRequestScope(), then it works fine - but then that causes problems with Entity Framework since objects are modified in multiple separate instances of the data context.

推荐答案

当然,发布的内容后立即单击在我的脑海里,我能解决这个问题。

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

问题在于这样一个事实:ActionFilters的行为MVC3发生了变化,我有一个过滤器,有我ProductService注入。

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.

我想,过滤器设置在服务和最终处置的的DbContext的。

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

在我的情况下,解决的办法很简单。我创建了一个为我的过滤器专门用于第二的DbContext。由于过滤器确实没有什么比查询多选择几个表来验证授权特定的资源,我也没必要说的DbContext跨越单个要求提供工作环境的单位。我创建了一个使用新的DbContext的新服务。在这种情况下,它是足以与InTransientScope被配置()

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()

这篇关于的DbContext设置的第一请求后,在使用Ninject的InRequestScope()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆