Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that you create and also close the session in you models <code>GetById</code> method. (the using statement closes the session) The session must be available during the whole business transaction.</p> <p>There are several ways to achieve this. You can configure NHibernate to use the session factories GetCurrentSession method. See <a href="http://nhibernate.info/doc/nhibernate-reference/architecture.html#architecture-current-session" rel="nofollow noreferrer">this on nhibernate.info</a> or <a href="http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx" rel="nofollow noreferrer">this post on Code Project</a>.</p> <pre><code>public SomeModel GetById(Guid id) { // no using keyword here, take the session from the manager which // manages it as configured ISession session = NHibernateSessionManager.Instance.GetSession(); return session.Get&lt;SomeModel &gt;(id); } </code></pre> <p>I don't use this. I wrote my own transaction service which allows the following:</p> <pre><code>using (TransactionService.CreateTransactionScope()) { // same session is used by any repository var entity = xyRepository.Get(id); // session still there and allows lazy loading entity.Roles.Add(new Role()); // all changes made in memory a flushed to the db TransactionService.Commit(); } </code></pre> <p>However you implement it, sessions and transactions should live as long as a business transaction (or system function). Unless you can't rely on transaction isolation nor rollback the whole thing.</p>
 

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