私はEFとNinjectの両方に慣れていないので、これが意味をなさない場合は許してください:)
NinjectおよびNinject.Web.Common参照を持つMVC3アプリケーションがあります。リポジトリにDbContextを挿入しようとしています。私が見ているのは、最初のリクエストではすべてがうまく機能しますが、後続のリクエストは次のように返されるということです。
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;}
...
}
My SiteDataContextクラス:
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()を削除すると、正常に機能しますが、オブジェクトがデータコンテキストの複数の個別のインスタンスで変更されるため、EntityFrameworkで問題が発生します。