简体   繁体   English

如何在流畅的nhibernate映射中使用数据库过程

[英]How to use database procedure in fluent nhibernate mapping

I have this class 我有这门课

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

and in below mapping, I fill in the value of 'Value' using a procedure in a Formula() 在下面的映射中,我使用Formula()的过程填充'值'的值

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

But it returns the error when executing: 但它在执行时返回错误:

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

Is there any way to use procedure in fluent nhibernate? 有没有办法在流利的nhibernate中使用程序?

The formula mapping expression is later converted by NHibernate to statement like this 公式映射表达式稍后由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)

The this_. 这个_。 prefix is injected at places, where NHibernate thinks it should properly use MAIN table alias. 在地方注入前缀,NHibernate认为它应该正确使用MAIN表别名。

This is not what we want.. and the only way I found, is to introduce our own Dialect (I am working with SQL Server) and define some "CONSTANTS" to be treated as - I do not need prefix 这不是我们想要的......而且我找到的唯一方法是介绍我们自己的Dialect(我正在使用SQL Server)并定义一些“CONSTANTS”来对待 - 我不需要前缀

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

This must be used for configuration 这必须用于配置

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

And finally, we have to adjust our statement 最后,我们必须调整我们的陈述

// 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)

NOTE: keywords, my experience, must be just lower case 注意:关键字,我的经验,必须只是小写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM