NHibernate方言的自定义SQL函数 [英] Custom SQL function for NHibernate dialect

查看:296
本文介绍了NHibernate方言的自定义SQL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够调用一个名为recent_date的自定义函数作为我的HQL的一部分。像这样: [Date]> = recent_date() p
$ b 我创建了一个新的方言,继承自MsSql2000Dialect,指定方言为我的配置。

  public class NordicMsSql2000Dialect:MsSql2000Dialect 
{
public NordicMsSql2000Dialect()
$ b RegisterFunction(
recent_date,
new SQLFunctionTemplate(
NHibernateUtil.Date,
dateadd(day,-15,getdate())

);



var configuration = Fluenly.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'不是一个可识别的函数名称



它在一个HasMany映射的where语句中,如下所示。

$ p $ HasMany(x => x.RecentValues)
.Access.CamelCaseField(Prefix.Underscore)
.Cascade.SaveUpdate()
.Where(Date> = recent_date());

我在这里错过了什么?

解决方案我认为,其中是一个SQL语句,而不是一个HQL语句。所以它不知道这个功能。它只适用于HQL,查询或过滤器。


I want to be able to call a custom function called "recent_date" as part of my HQL. Like this: [Date] >= recent_date()

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

When calling recent_date() I get the following error: System.Data.SqlClient.SqlException: 'recent_date' is not a recognized function name

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

What am I missing here?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆