views:

176

answers:

3

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?!

+1  A: 

The exception probably means that NHibernate tried to access Company.ID via reflection but the Company object was null, hence "requires a target" - the target was null.

I can't tell the reason why. But I can see that you use Linq-to-NHibernate. I also got null reference from it couple of times. I just switched to HQL and never bothered, because I can easily test my queries, and I still consider Linq-to-NHibernate a bit "unfinished".

Try to rewrite Linq, for example

return from t in _session.Linq() from c in t.Companies where c == company select t;

maybe it doesn't like Contains. Or just use HQL/ICriteria.

queen3
That still doesn't explain why it works the first time it's hit though.I tried your example, but got an error:could not resolve property: c of: CCMvc.Core.Entities.CarrierI'm still a bit of a newb with linq, so I don't really know how to fix that.
cadmium
+1  A: 

This may be part of another situation, but I've bumped into that error a couple times in the past with fluent mappings.

public CarrierMap() {
        Id(x => x.ID);
        Map(x => x.Name);
        HasManyToMany(x => x.Companies)
            .Table("CompanyCarrier")
            .LazyLoad();
    }

Adding LazyLoad() to the end of the mapping is something that has cleared this up for my personal projects.

Alexis Abril
A: 

Gah! I figured it out, via some other code I ended up passing a null value into the query, hence the crashing. Thank you all for your help!

cadmium