2015-06-21 29 views
1

我有一個ASP.NET MVC Code First站點。我有一個型號Teacher,它有一個列表Student s,其中每個都有一個計算屬性CompletedTests。 我越來越想包含的Student是清單一個ViewModel內訪問計算財產CompletedTests如果出現以下錯誤:MVC CodeFirst計算屬性獲取錯誤「ObjectContext實例處置」

An exception of type 'System.ObjectDisposedException' occurred in EntityFramework.dll but was not handled in user code 

Additional information: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. 

老師:

public class Teacher 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public virtual ICollection<Student> Students{ get; set; } 
} 

學生:

public class Student 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int TeacherId { get; set; } 

    public virtual Teacher Teacher { get; set; } 

    public int TotalTests 
    { 
     get 
     { 
      return Tests.Count; 
     } 
    } 

    public int CompletedTests 
    { 
     get 
     { 
      return Tests.Count(p => p.IsCompleted); 
     } 
    } 

    public virtual ICollection<Test> Tests{ get; set; } 
} 

測試:

public class Test 
{ 
    public int Id { get; set; } 

    public int StudentId { get; set; } 

    public int QuestionsTotal 
    { 
     get 
     { 
      return Questions.Count; 
     } 
    } 

    public bool IsCompleted 
    { 
     get 
     { 
      return Questions.Count(q => q.Completed) >= QuestionsTotal; 
     } 
    } 

    public virtual Student Student { get; set; } 
    public virtual ICollection<Question> Questions{ get; set; } 
} 

問:

public class Question 
{ 
    public int Id { get; set; } 
    public int TestId { get; set; } 

    public virtual Question Question { get; set; } 
} 

HomeViewModel:

public class HomeViewModel 
{ 
    public string TeacherName { get; set; } 
    public int StudentCount { get; set; } 
    public IEnumerable<Student> Students { get; set; } 
} 

首頁控制器:

public ActionResult Index() 
{ 
    var model = new HomeViewModel(); 
    var userId = User.Identity.GetUserId(); 
    using (var context = new ApplicationDbContext()) 
    { 
     var teacher = context.Teachers.FirstOrDefault(c => c.IdentityId == userId); 
     if (teacher != null) 
     { 
      model.TeacherName = teacher.Name; 
      model.Students = teacher.Students.ToList(); 
     } 
     return View(model); 
    } 
} 

Index.cshtml部分:

 <tbody> 
      @foreach (var student in Model.Students) 
      { 
       <tr> 
        <td> 
         @student.Name 
        </td> 
        <td> 
         @(student.CompletedTests + "/" + student.TotalTests) 
        </td> 
       </tr> 
      } 
     </tbody> 

難道有人請指出爲什麼我在student.CompletedTests部件上收到處置的實例錯誤?

+0

我看不到你在哪裏得到老師或學生的名字。您只有'Test'中的'Name'字段。 –

+0

對不起,有錯的課。它在'Student'上,而不是'Test'。 – Marcus

+0

'teacher.Name'呢? –

回答

1

使用包括加載學生的實體時避免延遲加載,包括測試實體:

model.Students = teacher.Students.Include(t => t.Tests).ToList(); 

這就是所謂的預先加載:

https://msdn.microsoft.com/en-us/data/jj574232.aspx

你得到的錯誤是因爲ObjectContext的(ApplicationDbContext)在您的視圖中不再可用。它已經置於控制器的方法內部。

相關問題