c# - 尝试传递 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.
我有一个功能,可以让我查看驾驶教练的应用程序并且工作正常,它使用类似的方法在创建表单中询问特定的驾驶教练。然后我有一个功能,列出了驾驶教练的应用程序。
这是代码。一些要点是:
- 用户类中的主键 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;
}
}
导致 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
解决方案
您的应用程序类不正确。它没有 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);
}
推荐阅读
- javascript - 如何在 Extjs 中转义 html 标签?
- extjs - ExtJs 6.7 - 在 textarea 中写入文本时,按 TAB 键不会退出 textarea 字段
- python - Tkinter 按钮未出现在窗口中
- css - Bootstrap 4 - 没有任何全局样式的 CSS
- doctrine-orm - 从字符串到实体的学说迁移
- sql - Redshift SQL-如何按所有列分组但不为每个日期创建单独的行。请选择最新日期
- c++ - 在 C++ 中读取/写入二进制文件
- c++ - 这应该是 constexpr 还是不是?
- javascript - 使用无效电子邮件从 googlesheet 停止循环发送电子邮件。如何跳过
- java - Docker找不到生成的jar文件