首页 > 解决方案 > 尝试传递 id 时出现 ASP.NET MVC ObjectDisposedException

问题描述

我要做的是创建一个函数,该函数将传递用户的 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.

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

这是代码。一些要点是:

如果您需要更多代码,请通知我。

打开 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;
            }
        }

导致 GetApplications 的 GetUser 的 HTML 视图,模型使用 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; }

}

用户服务获取用户功能:

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

标签: c#asp.net-mvc

解决方案


您的应用程序类不正确。它没有 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);
    }


推荐阅读