我是实体框架和ASP.NET的新手。我正在尝试访问导航属性的属性。但我面临“已经处置了ObjectContext实例”的错误。比我做了一些谷歌,发现它可以由data = db.tblCourses.Include(c => c.tblSection).ToList();
修复,但我仍然面临问题。
型号:
using System.Data.Entity;
public List<tblCourse> editedCoursesView()
{
List<tblCourse> data;
using (enrolmentsystemEntities db = new enrolmentsystemEntities())
{
data = db.tblCourses.ToList();
data = db.tblCourses.Include(c => c.tblSection).ToList();
return data;
}
}
tblCourse:
namespace enrolmentSystem.Models
{
using System;
using System.Collections.Generic;
public partial class tblCourse
{
public tblCourse()
{
this.tblenrolments = new HashSet<tblenrolment>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public int departmentID { get; set; }
public int TeacherId { get; set; }
public int CreditHours { get; set; }
public int SectionId { get; set; }
public virtual tbldepartment tbldepartment { get; set; }
public virtual tblteacher tblteacher { get; set; }
public virtual ICollection<tblenrolment> tblenrolments { get; set; }
public virtual tblSection tblSection { get; set; }
}
}
tblRoom:
namespace enrolmentSystem.Models
{
using System;
using System.Collections.Generic;
public partial class tblRoom
{
public tblRoom()
{
this.tblSections = new HashSet<tblSection>();
}
public int RoomNo { get; set; }
public string Block { get; set; }
public int Capacity { get; set; }
public virtual ICollection<tblSection> tblSections { get; set; }
}
}
控制器:
[HttpGet]
public ActionResult editCourse()
{
course c = new course();
List<tblCourse> data = c.editedCoursesView();
return View("editCourse", data);
}
查看:
@model List<enrolmentSystem.Models.tblCourse>
@{
ViewBag.Title = "editCourse";
}
<h2>Edit Course</h2>
<table border="1">
<tr style="text-align:center;">
<td>Course ID</td>
<td>Course Name</td>
<td> Course Department ID</td>
<td> Course Teacher ID</td>
<td> Course CreditHours</td>
<td> Course StartTime</td>
<td> Course EndTime</td>
<td> MaxCapacity </td>
<td> Course RoomNo </td>
</tr>
@if (Model != null)
{
foreach (var i in Model)
{
<form action="~/Enrolment/editCourse" method="post">
<tr>
<td>@i.CourseId <input name="id" type="hidden" value="@i.CourseId" /> </td>
<td><input type="text" name="name" value="@i.CourseName" required /></td>
<td><input type="number" name="dpId" value="@i.departmentID" required /></td>
<td><input type="number" name="teacherId" value="@i.TeacherId" required /></td>
<td><input type="number" name="creditHours" value="@i.CreditHours" required /></td>
<td><input type="text" name="startTime" value="@i.tblSection.startTime" required /></td>
<td><input type="text" name="endTime" value="@i.tblSection.endTime" required /></td>
<td><input type="number" name="capacity" value="@i.tblSection.tblRoom.Capacity" required /></td>
<td><input type="number" name="roomNo" value="@i.tblSection.RoomNo" required /></td>
<td> <input type="submit" value="Update Course" /> </td>
</tr>
</form>
}
}
</table>
答案 0 :(得分:0)
tblRoom
可能会导致问题,因为它不包含但在视图中引用。通常,此错误意味着您正在尝试延迟加载某些属性,但已经处理了上下文。
使用.Include(x => x.ApplicationsWithOverrideGroup.NestedProp)
或.Include(x => x.Collection.Select(y => y.Property))
包含嵌套属性或集合。
在您的情况下使用:
public List<tblCourse> editedCoursesView()
{
List<tblCourse> data;
using (enrolmentsystemEntities db = new enrolmentsystemEntities())
{
data = db.tblCourses
.Include(c => c.tblSection)
.Include(c => c.tblSection.tblRoom)
.ToList();
return data;
}
}