2016-01-23 81 views
-1

我是新的實體框架和ASP.NET。我正在嘗試訪問導航屬性的屬性。但我面臨着「ObjectContext實例已被處置」的錯誤。比我做了一些谷歌,發現它可以通過data = db.tblCourses.Include(c => c.tblSection).ToList();修復,但我仍然面臨這個問題。 型號:ASP.NET :: ObjectContext實例已配置

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

爲什麼你在做'ToList()'兩次在'editedCoursesView()'方法? –

回答

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; 
    } 
} 
+0

在第二個選項中是否存在收集手段表和屬性表示該表的字段? –

+0

當你在你的模型中有簡單的屬性時使用'Include(x => x.foo.bar)',當'foo'包含'Include(x => x.foo.select(y => y.bar))'是對象的集合,例如'列表',並且這些對象中的每一個都具有您想要加載的'bar'屬性。 – csharpfolk

+0

抱歉可以給我一些例子從我的代碼。我嘗試,但它給錯誤。 –

相關問題