I have two entities, Customer and Account. A Customer has many accounts.
My mapping for Customer is :
<bag cascade="all" name="Accounts" table ="Accounts" mutable="true" inverse="true">
<key>
<column name="Customer_Id" />
</key>
<one-to-many class="Account, POCOEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
My mapping for Account is:
<many-to-one cascade="all" class="Customer, POCOEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Customer">
<column name="Customer_Id" />
</many-to-one>
In my application, I retrieve a customer based on their ID:
var customer = _customerRepository.GetById(custID);
I then try to get the customers first account with:
Account account = customer.Accounts.FirstOrDefault();
I then receive the following exception: "NHibernate.LazyInitializationException: illegal access to loading collection"
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:
var acc = from a in _accountRepository.GetAll()
where a.Customer.Equals(customer)
select a;
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.
Anyone have any ideas what's going on here?