如何在Fluent NHibernate映射中使用数据库存储过程

6
我有这个类
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));
    }
}

但是执行时会返回错误:
{"Dynamic SQL Error\r\nSQL error code = -104\r\nToken unknown - line 1, column 279\r\n."}

有没有办法在Fluent NHibernate中使用存储过程?

你使用哪种数据库?也许这个链接可以解决你的问题:http://www.iprogrammable.com/2009/09/05/get-oracle-stored-procedure-output-object-output-in-nhibernate/ - tykovec
谢谢您的回答,我已经尝试过这种方式,但是没有成功。问题出在存储过程的别名上,使用的是 Firebird 数据库。 - Rodrigo Martins Cirqueira
1个回答

5
公式映射表达式稍后由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认为应该正确使用主表别名的地方注入的。

这不是我们想要的...我找到的唯一方法是引入自己的方言(我正在使用SQL Server)并定义一些“常量”来处理-我不需要前缀

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)

注意:关键词和我的经验必须全部是小写字母


谢谢你的回答。我正在使用Fluent Nhibernate。我需要理解这个问题,但是我对'Fluent'还很陌生,不知道在哪里可以创建这个别名,你知道是否可以使用'Fluent'来创建它吗? - Rodrigo Martins Cirqueira
1
唯一的变化(在流畅性方面)将是设置方言的方式。请查看此链接:http://nhibernate.info/blog/2008/09/06/a-fluent-interface-to-nhibernate-part-4-configuration.html。示例代码如下:var cfg = new MyConfiguration() .ShowSql() .Driver() .Dialect(); - Radim Köhler
1
t.VALOR_IND 应该改为 my_table_alias.VALOR_IND 吗?在 OP 的情况下,它的基本方言将是 FirebirdDialect。是的,关键字应该以小写形式注册。有点不幸的是,它目前没有被 RegisterKeyword 自己调整。但是,只要它们以小写形式注册,它们就会在查询中匹配任何大小写。 (候选标记在匹配之前会“小写”)。 - Frédéric
问题已解决!!非常感谢Radim Köhler和Frédéric。多亏了你们,我才能够解决这个问题。 - Rodrigo Martins Cirqueira

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接