gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:37:48 24 4
gpt4 key购买 nike

我有这门课

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中使用procedure吗?

最佳答案

公式映射表达式稍后被 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_. 前缀被注入(inject)到 NHibernate 认为它应该正确使用 MAIN 表别名的地方。

这不是我们想要的..我发现的唯一方法是引入我们自己的方言(我正在使用 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)

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

关于c# - 如何在 fluent nhibernate 映射中使用数据库过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44445367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com