I am having trouble setting up a Fluent NHibernate HasMany collection.
I have set the code up as below and I’m calling it via Linq IQueryable.
In SQL Profiler, I can see the correct SQL getting called, but the Store.Staff collection is always empty.
public class Store
{
public virtual IList<Employee> Staff { get; set; }
public virtual void AddEmployee(Employee Employee)
{
Employee.Store = this;
if(Staff == null)
Staff = new List<Employee>();
Staff.Add(Employee);
}
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.StoreId)
.GeneratedBy.Identity();
HasMany(x => x.Staff)
.Inverse()
.Cascade.All();
...
}
}
public bool Create(Store entity)
{
var stores = _readRepository.Query<Store>()
.Where(x => x.StoreId == entity.StoreId)
.Fetch(x => x.Staff)
.ToList();
select store0_.StoreId,
staff2_.SurgeryId,
staff2_.StoreId
from dbo.[Store] store0_
left outer join dbo.[Employee] staff2_
on store0_.StoreId = staff2_.StoreId
where store0_.StoreId = 1 /* @p0 */
Thanks for any help.
I was confusing two parts of the framework. I was using Linq to retrieve the data and couldn’t eagerly load.
Instead of using Linq, I now use NHibernate.Session.QueryOver.