我的博客
个人资料:
AlanThinker
AlanThinker@stk.me

NHibernate.Linq中使用自定义sql函数的方法.

软件开发 发表时间:2016-10-21 更新时间:2016-11-28

自定义一个Dialect, 然后再NHibernate的配置文件中使用此Dialect即可.    
然后再 linq 里面可以直接使用 MyPostgreSQLDialect.date_trunc​
 
using NHibernate;
using NHibernate.Dialect;
using NHibernate.Dialect.Function;
using NHibernate.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyProject.Biz
{
    public class MyPostgreSQLDialect : PostgreSQL82Dialect
    {
        static log4net.ILog log = log4net.MyLogManager.GetCurrentNamespaceLogger();

        public MyPostgreSQLDialect()
        {
            RegisterFunction("date_trunc", new StandardSQLFunction("date_trunc", NHibernateUtil.Date/*函数的返回值*/)); 
        }

        [LinqExtensionMethod]
        public static DateTime date_trunc(string type, DateTime time)
        {
            throw new NotImplementedException();//无需具体实现. 即可在 NHibernate.Linq中使用了.
        }
    }
}

IP Address: 43.129.217.254