【问题标题】:ASP.NET MVC ObjectDisposedException when trying to pass id尝试传递 id 时出现 ASP.NET MVC ObjectDisposedException
【发布时间】:2021-01-28 16:57:44
【问题描述】:

我要做的是创建一个函数,该函数将传递用户的 id,然后加载一个页面,显示用户向每位驾驶教练所做的所有应用程序。

我已经创建了一个函数,该函数将显示数据库中的所有应用程序,而不管是谁创建了应用程序。我还制作了一个允许用户创建应用程序的功能。我还在应用程序表中创建了一个名为 User_UserId 的字段,因此当创建应用程序并将其提交到数据库时,它将添加创建应用程序的特定 userId。

我的问题是,我将如何做到这一点,以便当您单击显示特定用户帐户上的所有应用程序时,它只会拉出附加了该 User_UserId 的应用程序。我可以将 UserId 添加到存储输入表单的类中并将其中的 UserId 设置为正在创建应用程序的 UserId 吗?

到目前为止,我的尝试都以在线用户控制器中的此错误告终 - IList<Application> applications = user.Applications.ToList();:

An exception of type 'System.ObjectDisposedException' occurred in EntityFramework.dll but was not handled in user code
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

我有一个功能,可以让我查看驾驶教练的申请并且运行良好,它使用类似的方法在创建表单中询问特定的驾驶教练。然后我有一个功能,列出了驾驶教练的应用程序。

这里是代码。需要说明的一些要点是:

  • 用户类中的主键UserId设置为字符串
  • 在存储输入数据的类中,没有UserId字段,因为该字段是在方法中传递的。
  • Applications 表中存储用户 ID 的字段名为 User_UserId

如果您需要更多代码,请告诉我。

为 GetApplications 打开视图的用户控制器:

    public ActionResult GetApplications(string id)
    {
        User user = userService.GetUser(id);
        IList<Application> applications = user.Applications.ToList();
        return View(applications);
    }

IDAO/DAO 文件:

IList<Application> GetApplications(string id, XContext context);

        public IList<Application> GetApplications(string id, XContext context)
    {
        User user = context.Users.Find(id);
        return user.Applications.ToList();
    }

IService/服务文件:

IList<Application> GetApplications(string id);

        public IList<Application> GetApplications(string id)
        {
            using (var context = new XContext())
            {

                IList<Application> Applications;
                Applications = userDAO.GetApplications(id, context);
                return Applications;
            }
        }

GetUser 的 HTML 视图导致 GetApplications,Model 使用 User 类:

    @Html.ActionLink("Show Applications", "GetApplications", "User") ||

上下文类:

    public class XContext : DbContext
{
    public XContext() : base("XContext")
    {
        Database.SetInitializer(new XInitializer());
    }
    public DbSet<User> Users { get; set; }
    public DbSet<Application> Applications { get; set; }
    public DbSet<Instructor> Instructors { get; set; }

}

UserService 获取用户函数:

public User GetUser(string id)
{
    using (var context = new XContext())
    {
        return userDAO.GetUser(id, context);
    }
}

UserDAO GetUser 函数:

        public User GetUser(string id, XContext context)
    {
        return context.Users.Find(id);
    }

用户类:

   using System.ComponentModel.DataAnnotations;
   public class User
    {
        [Key]
        public string UserId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public virtual ICollection<Application> Applications { get; set; }
        public virtual ICollection<Instructor> Instructors { get; set; }
    }

应用程序类:

public class Application
{
    [Key]
    public int ApplicationId { get; set; }
    public string Level { get; set; }
    public string Manual { get; set; }
    public string InstructorName { get; set; }
}

我在中断的控制器函数上设置了断点,结果如下:

        User user = userService.GetUser(id);

        id = "1"
        user = null

        IList<Application> applications = user.Applications.ToList();

        id = "1"
        applications = null

当我继续并抛出错误时,我得到了

        id = "1"
        applications = null
        user.Application = null

【问题讨论】:

  • 欢迎来到 SO。为了更清楚一点:你是在问为什么你会收到ObjectDisposedException?或者为什么你没有得到任何结果?我不确定我应该在这里回答什么。
  • @Xerillio 你好!谢谢你欢迎我,抱歉没有这么清楚。两者兼而有之,我想我明白为什么会抛出错误。我猜这是因为在使用此方法之前正在处理上下文类,因此它无法为用户获取应用程序列表,因为与数据库的连接已关闭。我主要是问如何解决引发错误的问题,因为我自己似乎无法找到解决方案。我已经添加了上下文类以防万一。
  • 请问 userService.GetUser 好吗?
  • @Sergey 当然。我现在已经为你添加了。感谢观看
  • 谢谢,但我什么也没看到。请显示返回 userDAO.GetUser(id, context);谢谢。

标签: c# asp.net-mvc


【解决方案1】:

您的应用程序类不正确。它没有 UserId

public class Application
{
    [Key]
    public int ApplicationId { get; set; }
    public string UserId { get; set; }
    public string Level { get; set; }
    public string Manual { get; set; }
    public string InstructorName { get; set; }
}

而且我认为您的 Instructor 课程中存在相同的错误

你的 DAO 中有错误。

替换

    public User GetUser(string id, XContext context)
    {
        return context.Users.Find(id);
    }

与

    public User GetUser(string id, XContext context)
    {
        return context.Users.Include(i=> i.Applications).FirstOrDefault(i=>i.UserId==id);
    }

然后替换

        public IList<Application> GetApplications(string id, XContext context)
    {
        User user = context.Users.Find(id);
        return user.Applications.ToList();
    }

与

    public IList<Application> GetApplications(string id, XContext context)
    {
        return context.Applications.Where(i=>i.UserId==id).ToList();
    }

用户服务代码:

public List<Application>  GetApplications(string id)
{
    using (var context = new XContext())
    {
        return userDAO.GetApplications(id, context);
    }
}


您的控制器代码:


public ActionResult GetApplications(string id)
    {
        var applications = userService.GetApplications(id);
        return View(applications);
    }

【讨论】:

  • 在 dao 中使用它对我不起作用,在您声明“i=>i.Id=id”的地方,我必须将 i.id 更改为 i.UserId,因为它就是这样在用户类中命名。我收到 2 个错误:“无法将类型 'string' 隐式转换为 'bool'”以及 -“无法将 lambda 表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型”
  • @DerekJo23 这是一个错字。我修好了它 。将“i.UserId=id”替换为 i.UserId==id
  • 谢谢,它已经加载了页面,但是用户的 id 仍然没有被传递,因为它正在将所有用户的所有应用程序加载到列表中。是不是因为它需要的用户 ID 位于名为 User_UserId 的 Application 类数据库表中,而不是来自 User 类的 UserId?或者您是否尝试将用户类中的 UserId 与 Application 数据库中的 User_UserId 匹配?
  • 你能告诉你dbcontext用户类吗?
  • 我现在已经为你添加了,谢尔盖。
猜你喜欢
  • 1970-01-01
  • 2012-06-10
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多