title: NHibernate - illegal access to loading collection
body: <p>I have two entities, Customer and Account. A Customer has many accounts. </p>
  
  <p>My mapping for Customer is :</p>
  
  <pre><code>&lt;bag cascade="all" name="Accounts" table ="Accounts" mutable="true" inverse="true"&gt;
    &lt;key&gt;
      &lt;column name="Customer_Id" /&gt;
    &lt;/key&gt;
    &lt;one-to-many class="Account, POCOEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /&gt;
  &lt;/bag&gt;
  </code></pre>
  
  <p>My mapping for Account is:</p>
  
  <pre><code>&lt;many-to-one cascade="all" class="Customer, POCOEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Customer"&gt;
    &lt;column name="Customer_Id" /&gt;
  &lt;/many-to-one&gt;
  </code></pre>
  
  <p>In my application, I retrieve a customer based on their ID:</p>
  
  <pre><code>var customer = _customerRepository.GetById(custID);
  </code></pre>
  
  <p>I then try to get the customers first account with:</p>
  
  <pre><code>Account account = customer.Accounts.FirstOrDefault();
  </code></pre>
  
  <p>I then receive the following exception: "NHibernate.LazyInitializationException: illegal access to loading collection"</p>
  
  <p>I've tried other solutions for this problem posted here but nothing is working. Whats really odd is if I insert the following code just before I try to access the account then everything works:</p>
  
  <pre><code>var acc = from a in _accountRepository.GetAll()
                    where a.Customer.Equals(customer)
                    select a;
  </code></pre>
  
  <p>All I've done in the preceding code is create a variable that I don't even use. Somehow it causes the statement "Account account = customer.Accounts.FirstOrDefault();" to succeed though. </p>
  
  <p>Anyone have any ideas what's going on here?</p>