以下内容来自开源项目Funnelweb。我正在将它从SQL Express转换为SQL CE 4.0。发送到数据库的SQL包含逻辑OR(||)。这会导致SQL错误。 anyboy可以解释为什么会发生这种情况吗?
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
select tag0_.Id as Id7_, tag0_.Name as Name7_ from "Tag" tag0_ where tag0_.Name like ('%'||@p0||'%')
堆栈跟踪
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.NhQueryProvider.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:
答案 0 :(得分:3)
尝试继承MsSqlCe [40] Dialect并在构造函数中添加以下行:
RegisterFunction("concat",
new VarArgsSQLFunction(NHibernateUtil.String, "(", "+", ")"));
如果有效,请打开一张Jira票(http://jira.nhforge.org),将其作为MsSqlCeDialect的补丁。