DbContext disposed - C#
Формулировка задачи:
Добрый вечер. Пишу сайт codefirst+unitofwork. Запускаю на дебаг, из базы вытаскивается все, что нужно, загружается страница, радость, счастье. Обновляю страницу - выкидывает ошибку The operation cannot be completed because the DbContext has been disposed.
using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
{
var rep = uow.GetRepository<IProductRepository>();
var currentProduct = rep.Get(1);
}public class UnitOfWorkFactory : IUnitOfWorkFactory
{
public UnitOfWorkFactory(IUnityContainer container)
{
_container = container;
}
public IUnitOfWork CreateUnitOfWork()
{
return _container.Resolve<IUnitOfWork>();
}
private readonly IUnityContainer _container;
} public UnitOfWork(IUnityContainer container)
{
_container = container;
Context = _container.Resolve<DataContext>();
}
public TRepository GetRepository<TRepository>() where TRepository : class
{
return _container.Resolve<TRepository>();
}
public void Commit()
{
Context.SaveChanges();
}
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
Context.Dispose();
}
}
internal DataContext Context { get; set; }
private Boolean _disposed;
private readonly IUnityContainer _container;public ProductRepository(UnitOfWork unitOfWork)
:base(unitOfWork)
{
}
public Product Add(Product entity)
{
return DataBase.Add(entity);
}
public Product Remove(Product entity)
{
return DataBase.Remove(entity);
}
public Product Get(Int32 entityId)
{
return DataBase.FirstOrDefault(x => x.Id == entityId);
}internal BaseRepository(UnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
internal virtual IDbSet<TEntity> DataBase
{
get
{
return UnitOfWork.Context.Set<TEntity>();
}
}
internal UnitOfWork UnitOfWork { get; private set; }
Добавил код, ибо как я понимаю, телепатически помочь не получается
Решение задачи: «DbContext disposed»
textual
Листинг программы
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
// my binds
kernal.Bind<DbContext>.To<EFDbContext>().InRequestScope();
kernal.Bind<IUnitOfWork>.To<UnitOfWork>();
kernal.Bind<IUnitOfWorkFactory>.To<UnitOfWorkFactory>();
kernal.Bind<IRepository<Catagory>>.To<EFRepository<Catagory>>();
}
}