-1

I am new in entity framework and ASP.NET. i am trying to access attributes of navigation properties. but i am facing the "The ObjectContext instance has been disposed" error. than i have do some google and found that it can be fixed by the data = db.tblCourses.Include(c => c.tblSection).ToList(); but i am still facing the problem. Model:

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

Controller:

[HttpGet]
public ActionResult editCourse()
{
    course c = new course();
    List<tblCourse> data = c.editedCoursesView();
    return View("editCourse", data);    
}

View:

@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>
Hacan Champ
  • 53
  • 11

1 Answers1

0

tblRoom may cause a problem because it is not Included but referenced in the view. Generally this error means that you are trying to Lazy Load some property but context was already disposed.

Use .Include(x => x.ApplicationsWithOverrideGroup.NestedProp) or .Include(x => x.Collection.Select(y => y.Property)) to include nested property or collection.

In your case use:

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;
    }
}
csharpfolk
  • 4,124
  • 25
  • 31
  • in second option does there collection means table and property means field of that table? – Hacan Champ Jan 23 '16 at 08:32
  • Use use `Include(x => x.foo.bar)` when you have simple poperty in your model and `Include(x => x.foo.select(y => y.bar))` when `foo` is a collection of objects e.g. `List` and each of these objects has `bar` property that you also want to load. – csharpfolk Jan 23 '16 at 08:33
  • sorry can u give me some example from my code. i have try but its giving error. – Hacan Champ Jan 23 '16 at 08:45
  • What kind of error, compilation error - or context disposed exception? – csharpfolk Jan 23 '16 at 08:50
  • List data; data = db.tblCourses.Include(x => x.tblsection.select(y => y.section)); does it correct? – Hacan Champ Jan 23 '16 at 08:54
  • @HacanChamp I thought tblSection is a simple property not a collection, can you post your model? I made edit to my answer (there are now 3 includes - one for tblSection and for tblRoom and one for Capacity). – csharpfolk Jan 23 '16 at 08:57
  • sir model is already posted in a question. – Hacan Champ Jan 23 '16 at 09:21
  • I mean `tblCourse` class, and maybe class that represents `tblRoom` – csharpfolk Jan 23 '16 at 09:25
  • Does it work when you use last code block from my answer (I updated it), if not please try to run program under debugger and tell me on what line it throws exception. – csharpfolk Jan 23 '16 at 14:28