This is a weird one for me. I have a simple domain with 2 entities Company and Carrier. They have a m:m relation via another table.
I set up my Fluent mappings as so
public partial class Carrier {
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual IList<Company> Companies { get; set; }
}
public class CarrierMap : ClassMap<Carrier> {
public CarrierMap() {
Id(x => x.ID);
Map(x => x.Name);
HasManyToMany(x => x.Companies)
.Table("CompanyCarrier");
}
}
public partial class Company {
public virtual int ID { get; set; }
public virtual string CompanyName { get; set; }
}
public class CompanyMap : ClassMap<Company> {
public CompanyMap() {
Id(x => x.ID);
Map(x => x.CompanyName);
}
}
And I have a simple linq query to get the Carriers based on company
public IQueryable<Carrier> GetCarriersByCompany(Company company) {
return from t in _session.Linq<Carrier>()
where t.Companies.Contains(company) select t;
}
And call it all in a MVC controller Company company = _session.AuthenticatedSellerCompany; IList carriers = _carrierRepository.GetCarriersByCompany(company) .ToList();
Here's the thing. It works perfectly the first time, and fails every time after that, until I recompile again. So I know on some level the query and mappings work!
The error I get is :
Non-static method requires a target.
Stepping through the debugger and digging deeper I get :
PropertyAccessException
Exception occurred getter of CCMvc.Core.Entities.Company.ID
Further, if I reverse the mappings, put a IList Carriers on the Company entity, and try to get a filtered list of companies by a Carrier, it works every time, but I don't know why. There is nothing special about either table or the m:m table binding them.
Help?!