NHibernate 方言的自定义 SQL 函数 [英] Custom SQL function for NHibernate dialect
问题描述
我希望能够调用一个名为recent_date"的自定义函数作为我的 HQL 的一部分.像这样:[Date] >= recent_date()
I want to be able to call a custom function called "recent_date" as part of my HQL. Like this: [Date] >= recent_date()
我创建了一个新的方言,继承自 MsSql2000Dialect 并为我的配置指定了方言.
I created a new dialect, inheriting from MsSql2000Dialect and specified the dialect for my configuration.
public class NordicMsSql2000Dialect : MsSql2000Dialect
{
public NordicMsSql2000Dialect()
{
RegisterFunction(
"recent_date",
new SQLFunctionTemplate(
NHibernateUtil.Date,
"dateadd(day, -15, getdate())"
)
);
}
}
var configuration = Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2000
.ConnectionString(c => .... )
.Cache(c => c.UseQueryCache().ProviderClass<HashtableCacheProvider>())
.Dialect<NordicMsSql2000Dialect>()
)
.Mappings(m => ....)
.BuildConfiguration();
调用 recent_date()
时出现以下错误:System.Data.SqlClient.SqlException: 'recent_date' 不是可识别的函数名称
When calling recent_date()
I get the following error:
System.Data.SqlClient.SqlException: 'recent_date' is not a recognized function name
我在如下的 HasMany 映射的 where 语句中使用它.
I'm using it in a where statement for a HasMany-mapping like below.
HasMany(x => x.RecentValues)
.Access.CamelCaseField(Prefix.Underscore)
.Cascade.SaveUpdate()
.Where("Date >= recent_date()");
我在这里遗漏了什么?
推荐答案
我认为,Where
是一个 SQL 语句,而不是一个 HQL 语句.所以它不知道这个功能.它仅适用于 HQL、查询或过滤器.
I think, Where
is a SQL statement, not a HQL statement. So it doesn't know the function. It only works for HQL, in queries or filters.
这篇关于NHibernate 方言的自定义 SQL 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!