Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to fix a NHibernate lazy loading error "no session or session was closed"?
    primarykey
    data
    text
    <p>I'm developing a website with ASP.NET MVC, NHibernate and Fluent Hibernate and getting the error "<strong>no session or session was closed</strong>" when I try to access a child object.</p> <p>These are my domain classes:</p> <pre><code>public class ImageGallery { public virtual int Id { get; set; } public virtual string Title { get; set; } public virtual IList&lt;Image&gt; Images { get; set; } } public class Image { public virtual int Id { get; set; } public virtual ImageGallery ImageGallery { get; set; } public virtual string File { get; set; } } </code></pre> <p>These are my maps:</p> <pre><code>public class ImageGalleryMap:ClassMap&lt;ImageGallery&gt; { public ImageGalleryMap() { Id(x =&gt; x.Id); Map(x =&gt; x.Title); HasMany(x =&gt; x.Images); } } public class ImageMap:ClassMap&lt;Image&gt; { public ImageMap() { Id(x =&gt; x.Id); References(x =&gt; x.ImageGallery); Map(x =&gt; x.File); } } </code></pre> <p>And this is my Session Factory helper class:</p> <pre><code>public class NHibernateSessionFactory { private static ISessionFactory _sessionFactory; private static ISessionFactory SessionFactory { get { if(_sessionFactory == null) { _sessionFactory = Fluently.Configure() .Database(MySQLConfiguration.Standard.ConnectionString(MyConnString)) .Mappings(m =&gt; m.FluentMappings.AddFromAssemblyOf&lt;ImageGalleryMap&gt;()) .ExposeConfiguration(c =&gt; c.Properties.Add("hbm2ddl.keywords", "none")) .BuildSessionFactory(); } return _sessionFactory; } } public static ISession OpenSession() { return SessionFactory.OpenSession(); } } </code></pre> <p>Everything works fine, when I get <strong>ImageGallery</strong> from database using this code:</p> <pre><code>IImageGalleryRepository igr = new ImageGalleryRepository(); ImageGallery ig = igr.GetById(1); </code></pre> <p>But, when I try to access the <strong>Image</strong> child object with this code</p> <pre><code>string imageFile = ig.Images[1].File; </code></pre> <p>I get this error:</p> <p><strong>Initializing[Entities.ImageGallery#1]-failed to lazily initialize a collection of role: Entities.ImageGallery.Images, no session or session was closed</strong></p> <p>Someone know how can I fix this?</p> <p>Thank you very much!</p> <p><strong>Edit</strong></p> <p>My GetById method is:</p> <pre><code> public ImageGallery GetById(int id) { using(ISession session = NHibernateSessionFactory.OpenSession()) { return session.Get&lt;ImageGallery&gt;(id); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload