Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 无法完成该操作,因为DbContext已被释放_Asp.net Mvc 3_Entity Framework 4.1_Ninject_Httpmodule - Fatal编程技术网

Asp.net mvc 3 无法完成该操作,因为DbContext已被释放

Asp.net mvc 3 无法完成该操作,因为DbContext已被释放,asp.net-mvc-3,entity-framework-4.1,ninject,httpmodule,Asp.net Mvc 3,Entity Framework 4.1,Ninject,Httpmodule,我尝试进行自定义错误处理(http://perspectivespace.com/100497697)带有错误模块(应用程序错误)、HttpModuleMagic.MVC3、Ninject.MVC3、EF 4.1。不幸的是,当我想将错误记录到数据库中时,会显示以下错误消息: “无法完成该操作,因为DbContext已被释放。” 谁能帮帮我吗 public class ErrorHandlerHttpModule : IHttpModule { private const string E

我尝试进行自定义错误处理(http://perspectivespace.com/100497697)带有错误模块(应用程序错误)、HttpModuleMagic.MVC3、Ninject.MVC3、EF 4.1。不幸的是,当我想将错误记录到数据库中时,会显示以下错误消息: “无法完成该操作,因为DbContext已被释放。”

谁能帮帮我吗

public class ErrorHandlerHttpModule : IHttpModule
{
    private const string ErrorControllerRouteName = "Error";
    private IErrorRepository errorRepo;
    private IUserRepository userRepo;

  //  private IUserRepository UserRepository;

    public ErrorHandlerHttpModule(IErrorRepository er, IUserRepository ur)
    {
        errorRepo = er;
        userRepo = ur;
    }

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.Error += Application_Error;
    }

    private void Application_Error(object sender, EventArgs e)
    {
        // Get the last error.

        var exception = HttpContext.Current.Server.GetLastError();
    ...

    error.User = userRepo.GetUserByName(name);

    ...
在NinjectWebCommon.cs中:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IErrorRepository>().To<ErrorRepository>().InRequestScope();
        kernel.Bind<IUserRepository>().To<UserRepository>().InRequestScope();

        kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InRequestScope();

        kernel.Bind<IDisposable>().To<Disposable>().InRequestScope();
        kernel.Bind<IHttpModule>().To<ErrorHandlerHttpModule>();
    } 
该行显示错误:

dataContext.Users.Where(u => u.UserName.Equals(name)).FirstOrDefault() as User;
我尝试跟随这篇文章:


感谢您的回答。

原因是您的LINQ查询被延迟,换句话说,它不会在“UserRepository”中运行,而只在调用代码中运行。因此,DBContext在执行时已经被释放。您可以通过更改

protected override void DisposeCore()
{
   if (dataContext != null)
   {
      dataContext.Dispose();
      dataContext = null;
   }
}
us = dataContext.Users.Where(u => u.UserName.Equals(name)).FirstOrDefault() as User


更好的是,您在哪里创建了dataContext的实例?你在哪里关的?非常非常感谢。我有好几天都在为这件事发愁。这件事解决了吗?如果是的话,你能把你的代码贴出来让我看看你做了什么吗?我有类似的代码和类似的错误。谢谢:)我记得这是由Ninject的错误用法造成的。这是你在没有任何帮助的情况下所能得到的最大帮助。Ninject有一百万种不同的错误用法。想详细说明一下吗?
dataContext.Users.Where(u => u.UserName.Equals(name)).FirstOrDefault() as User;
protected override void DisposeCore()
{
   if (dataContext != null)
   {
      dataContext.Dispose();
      dataContext = null;
   }
}
us = dataContext.Users.Where(u => u.UserName.Equals(name)).FirstOrDefault() as User
us = dataContext.Users.Where(u => u.UserName.Equals(name)).ToList().FirstOrDefault() as User