NHibernate LINQ 查询抛出错误“无法解析属性”

发布于 2024-09-01 00:49:51 字数 2394 浏览 2 评论 0原文

我正在测试将 LINQ 与 NHibernate 结合使用,但在解析 string.length 时遇到了一些问题。我有以下

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();
        }
    }

在我的测试方法中我首先尝试使用 SQL 执行查询,这工作得很好。然后我想在 LINQ 中做同样的事情,它会抛出以下错误:

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

我已经搜索了很多这个“无法解析属性”错误,但我不太明白这意味着什么。这是因为LINQ实现不完整吗?如果是这样,那么来自 Linq2Sql 的结果有点令人失望,因为它可以正常工作。

我还尝试使用 hbm.xml 设置映射,而不是使用 FluentNHibernate,但它产生了相同的错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素衣风尘叹 2024-09-08 00:49:51

旧的 (2.x) Linq 提供程序不支持 String.Length 投影。

NHibernate 3.x 中集成了新的 Linq 提供程序;您无需下载任何额外的内容。

新的扩展方法是 session.Query 而不是 session.Linq;如果您使用后者,那么您仍在使用旧的提供商。

String.Length projections are not supported by the old (2.x) Linq provider.

The new Linq provider in NHibernate 3.x is integrated; you don't have to download anything extra.

The new extension method is session.Query instead of session.Linq; if you're using the latter, you are still using the old provider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文