问题描述
我想要做的是创建一个函数,该函数将传递用户的 id,然后加载一个页面,该页面显示用户为每位驾驶教练所做的所有应用程序。
我已经做了一个函数,无论是谁创建的应用程序,它都会显示数据库中的所有应用程序。我还做了一个功能,允许用户创建应用程序。我还在应用程序表中创建了一个名为 User_UserId 的字段,以便在创建应用程序并将其提交到数据库时,它将添加创建该应用程序的特定用户 ID。
我的问题是,我将如何做到这一点,以便当您单击显示特定用户帐户上的所有应用程序时,它只会拉出附加了该 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; }
}
UserService GetUser 函数:
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);
}