I'm currently learning ASP.NET with MVC3 using NHibernate and FluentNHibernate. I made myself a simple one-to-many test to try and retrieve a one to many relationship and put this into a poco structure.
As an example, I have a game entity and a developer entity, one developer can have multiple games and a game only has one developer.
I am basing my current views around the developer, and wish to see a list of games that were developed by them. When viewing the details of a single developer in the browser, I am getting the error:
Initializing[OneToManyTest.Models.Objects.Developer#1]-failed to lazily initialize a collection of role: OneToManyTest.Models.Objects.Developer.Game, no session or session was closed.I know what this means, namely that the Game data cannot be retrieved once the session was closed, which is why I am trying eager loading. This however, doesn't seem to work. The query NHibernate executes is ok, but my code will not properly fill the POCO objects.
The developer and game POCO classes look like this:
Code:
public class Developer
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
private IList<Game> game = new List<Game>();
public virtual IList<Game> Game
{
get { return game; }
set { game = value; }
}
}
public class Game
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int DeveloperId { get; set; }
public virtual Developer Developer { get; set; }
}
The fields of these classes match one-on-one with the database structure and column naming, except for the Developer field in the Game class (which is ofcourse non-existant in the database)
I have the following controller method for retrieving the data of a single developer:
Code:
public ActionResult Details(int id)
{
using (ISession session = sessionFactory.OpenSession())
{
//var developer = session.Get<Developer>(id);
var query = session.CreateCriteria<Developer>()
.SetFetchMode("Game", FetchMode.Eager)
.CreateCriteria("Game");
var developer = query.List<Developer>().FirstOrDefault<Developer>();
return View(developer);
}
}
The mapping to the data looks as follows:
Code:
public class DeveloperMap : ClassMap<Developer>
{
public DeveloperMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasMany<Game>(x => x.Game).Access.CamelCaseField(Prefix.None).KeyColumn("DeveloperId");
}
}
public class GameMap : ClassMap<Game>
{
public GameMap()
{
Id(x => x.Id);
Map(x => x.Name);
References<Developer>(x => x.Developer).Column("DeveloperId");
}
}
Furthermore the view fails when I try to access the Game field of a Developer object as such:
Code:
@model OneToManyTest.Models.Objects.Developer
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
@Model.Name
@Model.Game.Count
The error occurs on the
Code:
@Model.Game.Count
line
I have tried a lot of googling, fiddling and am completely out of options, so I hope that anyone would be willing to spend some time in this and help me :). If desired, i could send you a zip of the solution + create table queries.
Thanks a lot for your time!