NHibernate LINQ Contains 不起作用

发布于 2024-11-17 16:44:45 字数 1251 浏览 1 评论 0原文

我是 NHibernate 的新手,正在与 FluentNhibernate 合作开展一个新项目。在执行一个非常简单的 linq 查询时,我遇到了一个奇怪的问题。

查询 1 - 工作得很好

      //Query 1
          var customers = from customer in _session.Query<Customer>() 
                        where customer.FirstName == "john"
                        select new
                        {
                            customer.FirstName,
                            customer.LastName
                        };

当执行下面的查询 2 时,我收到错误“无法执行查询”

       //Query 2.
           var customers = from customer in _session.Query<Customer>() 
                         where customer.FirstName.Contains("john")
                        select new
                        {
                            customer.FirstName,
                            customer.LastName
                        };

NH 生成的查询是,

选择 customer0_.FirstName 作为 col_0_0_,customer0_.LastName 作为 col_1_0_ 来自 tblCustomer customer0_ 其中 customer0_.FirstName like ('%'||@p0||'%');@p0 = 'john'

它已生成 FirstName like ('%'||@p0||'%') 而不是 FirstName ('%'+@p0+'%')

我使用的数据库是SQLServerCE,NHibernate版本是3.1.0

请帮助解决这个问题。

I am new to NHibernate and working with FluentNhibernate for a new project. I came accross a strange issue when executing a very simple linq query.

Query 1 - Works very well

      //Query 1
          var customers = from customer in _session.Query<Customer>() 
                        where customer.FirstName == "john"
                        select new
                        {
                            customer.FirstName,
                            customer.LastName
                        };

When below query 2 is executed I get an error "Could not execute query"

       //Query 2.
           var customers = from customer in _session.Query<Customer>() 
                         where customer.FirstName.Contains("john")
                        select new
                        {
                            customer.FirstName,
                            customer.LastName
                        };

The NH generated query is,

select customer0_.FirstName as col_0_0_, customer0_.LastName as col_1_0_
from tblCustomer customer0_
where customer0_.FirstName like ('%'||@p0||'%');@p0 = 'john'

it has generated FirstName like ('%'||@p0||'%') instead of FirstName like ('%'+@p0+'%') as I can understand.

The database I am using is SQLServerCE and the NHibernate version is 3.1.0

Please help resolving this issue.

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

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

发布评论

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

评论(1

离鸿 2024-11-24 16:44:45

这是“Dialect.cs”文件(NH 的基本方言类)中 concat 函数的定义:

RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "||", ")"));

这应该

RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "+", ")"));

用于 SQL CE。
所以你必须使用MsSqlCe40Dialect 类。
我认为它还没有在 Fluent-NHibernate 中注册,所以你可以这样定义它:

... MsSqlCeConfiguration.Standard  
                      ...  
                      .Dialect<MsSqlCe40Dialect>();  

另外还有一个针对其余缺失功能的补丁,正在等待批准:
https://nhibernate.jira.com/browse/NH-2723
请投票!

同时,您可以创建自己的方言:

using NHibernate;
using NHibernate.Dialect;
using NHibernate.Dialect.Function;

namespace Test1
{
    public class TempSqlCeDialect : MsSqlCeDialect
    {
        public TempSqlCeDialect()
        {
            RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "+", ")"));
        }
    }
}

然后您可以通过定义来使用它:

... MsSqlCeConfiguration.Standard.Dialect<TempSqlCeDialect>() ...

This is the definition of concat function in "Dialect.cs" file (base dialect class of NH):

RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "||", ")"));

Which should be

RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "+", ")"));

for SQL CE.
So you have to use MsSqlCe40Dialect class.
I think it's not registered in the fluent-NHibernate yet, so you can define it this way:

... MsSqlCeConfiguration.Standard  
                      ...  
                      .Dialect<MsSqlCe40Dialect>();  

Also there is a patch for the rest of missing functions, waiting for approval:
https://nhibernate.jira.com/browse/NH-2723
Please vote!

In the meantime you can create your own dialect:

using NHibernate;
using NHibernate.Dialect;
using NHibernate.Dialect.Function;

namespace Test1
{
    public class TempSqlCeDialect : MsSqlCeDialect
    {
        public TempSqlCeDialect()
        {
            RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "+", ")"));
        }
    }
}

And then you can use it by defining:

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