簡體   English   中英

如何在流暢的nhibernate映射中使用數據庫過程

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

我有這門課

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

在下面的映射中,我使用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));
    }
}

但它在執行時返回錯誤:

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

有沒有辦法在流利的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)

這個_。 在地方注入前綴,NHibernate認為它應該正確使用MAIN表別名。

這不是我們想要的......而且我找到的唯一方法是介紹我們自己的Dialect(我正在使用SQL Server)並定義一些“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)

注意:關鍵字,我的經驗,必須只是小寫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM