【发布时间】: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