开发者

Nhibernate creating unusual SQL for a basic Query

开发者 https://www.devze.com 2023-02-17 20:29 出处:网络
The following is from the open source project Funnelweb.I am in the process of converting it from SQL Express to SQL CE 4.0.The SQL that is sent to the database contains logical OR ( || ).This results

The following is from the open source project Funnelweb. I am in the process of converting it from SQL Express to SQL CE 4.0. The SQL that is sent to the database contains logical OR ( || ). This results in the SQL error. Can anyboy explain why this would be happening?

FLuenbt Nhibernate Mapping

public class TagMapping : ClassMap<Tag>
    {
        public TagMapping()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            HasManyToMany(x => x.Entries)
                .Table("TagItem")
                .ParentKeyColumn("TagId")
                .ChildKeyColumn("EntryId")
                .AsSet()
                .Inverse()
                .LazyLoad();
        }
    }


public IQueryable<Tag> GetTags(string tagName)
        {
            tagName = tagName ?? string.Empty;

            return from tag in session.Query<Tag>()
                   where tag.Name.Contains(tagName)
                   select tag;

        }

SQL that it sent to query the database

select tag0_.Id as Id7_, tag0_.Name as Name7_ from "Tag" tag0_ where tag0_.Name like ('%'||@p0||'%')

Stack Trace

NHibernate.Exceptions.GenericADOException was unhandled by user code
  Message=could not execute query
[ select tag0_.Id as Id7_, tag0_.Name as Name7_ from "Tag" tag0_ where tag0_.Name like ('%'||@p0||'%') ]
  Name:p1 - Value:
[SQL: select tag0_.Id as Id7_, tag0_.Name as Name7_ from "Tag" tag0_ where tag0_.Name like ('%'||@p0||'%')]
  Source=NHibernate
  SqlString=select tag0_.Id as Id7_, tag0_.Name as Name7_ from "Tag" tag0_ where tag0_.Name like ('%'||@p0||'%')
  StackTrace:
       at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
       at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
       at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
       at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters)
       at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters)
       at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)
       at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
       at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)
       at NHibernate.Impl.ExpressionQueryImpl.List()
       at NHibernate.Linq.NhQueryPr开发者_开发百科ovider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
       at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
       at NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression expression)
       at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at FunnelWeb.Web.Areas.Admin.Views.WikiAdmin.EditModel..ctor(PageName page, Int32 originalEntryId, IEnumerable`1 tags) in C:\Projects\oss\funnelweb\src\FunnelWeb.Web\Areas\Admin\Views\WikiAdmin\EditModel.cs:line 24
       at FunnelWeb.Web.Areas.Admin.Controllers.WikiAdminController.Edit(PageName page, Nullable`1 revertToRevision) in C:\Projects\oss\funnelweb\src\FunnelWeb.Web\Areas\Admin\Controllers\WikiAdminController.cs:line 52
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: System.Data.SqlServerCe.SqlCeException
       Message=There was an error parsing the query. [ Token line number = 1,Token line offset = 91,Token in error = | ]
       Source=SQL Server Compact ADO.NET Data Provider
       ErrorCode=-2147467259
       HResult=-2147217900
       NativeError=25501
       StackTrace:
            at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
            at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()
            at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
            at System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior)
            at System.Data.SqlServerCe.SqlCeCommand.ExecuteDbDataReader(CommandBehavior behavior)
            at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
            at NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)
            at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session)
            at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
            at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
            at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
       InnerException: 


Try subclassing the MsSqlCe[40]Dialect and adding the following line in the constructor:

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

If it works, please open a Jira ticket (http://jira.nhforge.org) with it as a patch to MsSqlCeDialect.

0

精彩评论

暂无评论...
验证码 换一张
取 消