I'm currently learning ASP.NET with MVC using NHibernate and
FluentNHibernate and the Razor View Engine. 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

The developer and game classes look like this:

    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:

        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:
    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:

@model OneToManyTest.Models.Objects.Developer

@{
 ViewBag.Title = "Details";
}

<h2>Details</h2>

@Model.Name

@Model.Game.Count
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!

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to