Reputation: 699
Again I have problem with my project. I have two models created with EF 5 DBContext Generator:
First:
public int ID_AN { get; set; }
public string TITLE_OR { get; set; }
public string TITLE_EN { get; set; }
public virtual ICollection<GENRES> GENRES { get; set; }
Second:
public int ID_GE { get; set; }
public string GENRE { get; set; }
public virtual ICollection<ANIME> ANIME { get; set; }
After that I created controller:
public ActionResult Details(int id)
{
using (var db = new MainDatabaseEntities())
{
return View(db.ANIME.Find(id););
}
}
And View:
@model AnimeWeb.Models.ANIME
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<fieldset>
<legend>ANIME</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.TITLE_OR)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TITLE_OR)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.TITLE_EN)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.TITLE_EN)
</div>
</fieldset>
To this point everything works fine, but I would like to display all Genres of selected anime. When I try to add
<div>
@Html.DisplayFor(model => model.GENRES)
</div>
I get an error: "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."
I'm new to MVC so I would be very gratefull if someone could explain to me how to make it possible to work.
Upvotes: 0
Views: 733
Reputation: 596
you can use "include" extended method to load GENRES,like this:
db.ANIME.Include("GENRES").Find(id)
if you want to use @Html.DisplayFor(model => model.GENRES)
to show all of the generes, you can do it with DisplayTemplates,check this ASP.NET MVC display and editor templates.
Upvotes: 1
Reputation: 8595
Consider initializing the context when the controller is initialized, and disposing of it in the controller's dispose method.
This is the pattern (incidentally) that is used by the templates in visual studio.
public class DemoController : Controller
{
private MainDatabaseEntities db = new MainDatabaseEntities();
public ActionResult Details(int id)
{
return View(db.ANIME.Find(id));
}
protected override void Dispose(bool disposing)
{
this.db.Dispose();
base.Dispose(disposing);
}
}
Upvotes: 0
Reputation: 9244
This is an Entity Framework issue actually. Once you leave your Controller you are too late to be querying the database. Entity Framework is lazy loading (aka on demand) the Genres. To fix this you should change your query to explicitly load the Genres when retrieving the Anime object.
Update: Never told you how to do that. There are many ways to explicitly or eager load your Genres. The best resource for that would be to read the MSDN page on Loading Related Entities. One way would be to add code similiar to the following in your controller (I think this works with how you have it setup).
public ActionResult Details(int id)
{
using (var db = new MainDatabaseEntities())
{
var anime = db.ANIME.Find(id);
db.Entry(anime).Collection(a => a.GENRES).Load();
return View(anime);
}
}
Upvotes: 1