NHibernate支持功能:LEN [英] NHibernate support the function: LEN

查看:81
本文介绍了NHibernate支持功能:LEN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到结果:

select * from Cate where LEN(code)=2

我的代码:

var filter1 = Restrictions.Eq(
                 Projections.SqlFunction("LEN", NHibernateUtil.Int32,
                                         Projections.Property("code")), 2);
             var query =
                 repository.Session.QueryOver<Cate>().Where(filter1).List();
             Assert.IsTrue(query.Count > 0);

但是,出现了错误:

NHibernate.HibernateException:当前方言 NHibernate.Dialect.MsSql2008Dialect不支持该功能:LEN

NHibernate.HibernateException : Current dialect NHibernate.Dialect.MsSql2008Dialect doesn't support the function: LEN

如何在Nhibernate中使用Len函数的SQLServer2008?

How can I use in Nhibernate the SQLServer2008 of the Len function?

推荐答案

您应该在Configuration类中注册您的函数

You should register your function either in the Configuration class

config.AddSqlFunction("len", new StandardSafeSQLFunction("len", NHibernateUtil.Int32, 1));

或创建自定义方言

public class MyDialect : MsSql2008Dialect
{
    public MyDialect()
    {
        RegisterFunction("len", new StandardSafeSQLFunction("len", NHibernateUtil.Int32, 1));
    }
}

这篇关于NHibernate支持功能:LEN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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