如何在 fluent nhibernate 映射中使用数据库过程

How to use database procedure in fluent nhibernate mapping

我有这个class

public class Bill : EntityBase
{
      public virtual decimal Value { get; set; }
}

在下面的映射中,我使用 Formula()

中的过程填写 'Value' 的值
public class MapBill : ClassMap<Bill>
{
    public MapBill()
    {
        Table("cabrec");
        Map(m => m.Value)
            .Formula(
"(select t.VALOR_IND from ret_vlorind(1,1,cast('02/06/1993' as Date)) as t)")
            .CustomType(typeof(decimal));
    }
}

但是returns执行时报错:

{"Dynamic SQL Error\r\nSQL error code = -104\r\nToken unknown - line 1, column 279\r\n."}

有没有办法在fluent nhibernate中使用程序?

公式映射表达式后来被NHibernate转换成这样的语句

// source in code
(select t.VALOR_IND from ret_vlorind(1,1,cast('02/06/1993' as Date)) as t)
// sent SQL statement
(select t.VALOR_IND from ret_vlorind(1,1,cast('02/06/1993' as Date)) as this_.t)

this_. 前缀被注入到 NHibernate 认为应该正确使用 MAIN table 别名的地方。

这不是我们想要的..我发现的唯一方法是引入我们自己的方言(我正在使用 SQL 服务器)并定义一些 "CONSTANTS" 被视为- 我不需要前缀

public class CustomMsSql2012Dialect : MsSql2012Dialect
{
    public CustomMsSql2012Dialect()
    {
        RegisterKeyword("my_table_alias");
        ...

这必须用于配置

<property name="dialect">MyNamespace.CustomMsSql2012Dialect,MyLib</property>

最后,我们必须调整我们的声明

// wrong t is not known keyword.. would be prefixed
(select t.VALOR_IND from ret_vlorind(1,1,cast('02/06/1993' as Date)) as t)
// into this
(select my_table_alias.VALOR_IND from ret_vlorind(1,1,cast('02/06/1993' as Date)) as my_table_alias)

注意:根据我的经验,关键字必须小写