![](/img/trans.png)
[英]ASP.NET Core MVC - System.ObjectDisposedException
[英]ASP.NET MVC ObjectDisposedException when trying to pass id
我要做的是創建一個 function ,它將通過用戶的 id,然后加載一個頁面,顯示用戶對每個駕駛教練所做的所有應用程序。
我已經制作了一個 function,它將顯示數據庫中的所有應用程序,無論是誰創建了應用程序。 我還制作了一個 function 允許用戶創建應用程序。 我還在應用程序表中創建了一個名為 User_UserId 的字段,因此當創建應用程序並將其提交到數據庫時,它將添加創建應用程序的特定 userId。
我的問題是,我將如何做到這一點,以便當您單擊顯示特定用戶帳戶上的所有應用程序時,它只會拉出附加了該 User_UserId 的應用程序。 我可以將 UserId 添加到存儲輸入表單的 class 並將其中的 UserId 設置為正在創建應用程序的 UserId 嗎?
到目前為止,我的嘗試都以在線用戶 Controller 中的此錯誤結束 - 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.
我有一個 function 允許我查看駕駛教練的應用程序並且工作正常,它使用類似的方法在創建表單中詢問特定的駕駛教練。 然后我有一個 function 列出了駕駛教練的申請。
這是代碼。 一些要點是:
如果您需要更多代碼,請通知我。
打開 GetApplications 視圖的用戶 Controller:
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 使用用戶 class:
@Html.ActionLink("Show Applications", "GetApplications", "User") ||
上下文 class:
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; }
}
用戶服務 GetUser function:
public User GetUser(string id)
{
using (var context = new XContext())
{
return userDAO.GetUser(id, context);
}
}
UserDAO GetUser function:
public User GetUser(string id, XContext context)
{
return context.Users.Find(id);
}
用戶 Class:
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; }
}
應用 Class:
public class Application
{
[Key]
public int ApplicationId { get; set; }
public string Level { get; set; }
public string Manual { get; set; }
public string InstructorName { get; set; }
}
我在 controller function 上設置了斷點,結果如下:
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
您的應用程序 class 不正確。 它沒有 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; }
}
而且我認為您的講師 class 也有同樣的錯誤
你的 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);
}
}
您的 controller 代碼:
public ActionResult GetApplications(string id)
{
var applications = userService.GetApplications(id);
return View(applications);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.