2

In my repository I have:

public IQueryable<ICustomer> GetByAddress(string address)
{
    return from c in GetSession().Linq<ICustomer>()
           where c.Address.StartsWith(address)
           select c;
}

The SQL I'd like it to generate is essentially:

SELECT *
FROM Customer
WHERE address LIKE @address + '%'

However, whenever I do

var customers = myRepository.GetByAddress("123 Main Street");

I get a NullReferenceException:

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="NHibernate"
StackTrace:
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetEntityName(ICriteria subcriteria, String propertyName)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumns(String propertyName, ICriteria subcriteria)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumnsUsingProjection(ICriteria subcriteria, String propertyName)
Snip...

I have snipped the rest of the exception because it is quite verbose, if it would be helpful I can add it.

Why does this not work, while the exact same query except with

where c.Address == address

works perfectly fine.

Any ideas?

Edit

I'm using FluentNHibernate (where Id is a GUID and Address is a string):

public class CustomerMap: ClassMap<ICustomer>
{
    public JobMap()
    {
        Id(x => x.Id);
        ...snip...
        Map(x => x.Address).Not.Nullable();
    }
}
4

0 回答 0