NHibernate LINQ query throws error "Could not resolve property"

Posted by Xorandor on Stack Overflow See other posts from Stack Overflow or by Xorandor
Published on 2010-05-06T07:01:32Z Indexed on 2010/05/06 11:08 UTC
Read the original article Hit count: 903

I'm testing out using LINQ with NHibernate but have run into some problems with resolving string.length. I have the following

public class DC_Control
{
    public virtual int ID { get; private set; }
    public virtual string Name { get; set; }
    public virtual bool IsEnabled { get; set; }
    public virtual string Url { get; set; }
    public virtual string Category { get; set; }
    public virtual string Description { get; set; }
    public virtual bool RequireScriptManager { get; set; }
    public virtual string TriggerQueryString { get; set; }
    public virtual DateTime? DateAdded { get; set; }
    public virtual DateTime? DateUpdated { get; set; }
}

public class DC_ControlMap : ClassMap<DC_Control>
{
    public DC_ControlMap()
    {
        Id(x => x.ID);
        Map(x => x.Name).Length(128);
        Map(x => x.IsEnabled);
        Map(x => x.Url);
        Map(x => x.Category);
        Map(x => x.Description);
        Map(x => x.RequireScriptManager);
        Map(x => x.TriggerQueryString);
        Map(x => x.DateAdded);
        Map(x => x.DateUpdated);
    }
}

private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
            .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008)
            .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
            .ExposeConfiguration(c => c.SetProperty("connection.connection_string", "CONNSTRING"))
            .ExposeConfiguration(c => c.SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle"))
            .BuildSessionFactory();
    }

public static void test()
    {
        using (ISession session = sessionFactory.OpenSession())
        {
            var sqlQuery = session.CreateSQLQuery("select * from DC_Control  where  LEN(url) > 80").AddEntity(typeof(DC_Control)).List<DC_Control>();

            var linqQuery= session.Linq<DC_Control>().Where(c => c.Url.Length > 80).ToList();
        }
    }

In my test method I first try and perform the query using SQL, this works just fine. Then I want to do the same thing in LINQ, and it throws the following error:

NHibernate.QueryException: could not resolve property: Url.Length of: DC_Control

I've searched alot for this "could not resolve property" error, but I can't quite figure out, what this means. Is this because the LINQ implementation is not complete? If so it's a bit disappointing coming from Linq2Sql where this would just work.

I also tried it setting up the mapping with a hbm.xml instead of using FluentNHibernate but it produced teh same error.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about LINQ