gpt4 book ai didi

c# - 在 NHIbernate 中使用带有公式的属性映射

转载 作者:太空狗 更新时间:2023-10-29 18:27:27 25 4
gpt4 key购买 nike

我正在尝试将属性映射到另一个表的任意列。文档说公式可以是任意 SQL,我看到的例子也很相似。

但是,NHibernate 生成的 SQL 甚至是无效的。公式中的整个 SQL 语句被注入(inject)到 SELECT 语句的中间。

        Property(x => x.Content, map =>
{
map.Column("Content");
map.Formula("select 'simple stuff' as 'Content'");
});

最佳答案

这就是 Formula 的设计方式,它应该以这种方式工作。您需要将 SQL 语句包裹在括号中,以便生成有效的 SQL。

此外,您不能同时指定 Column 和 Formula。您必须提供完整的 SQL 语句。任何非前缀/转义列(下例中的“id”)将被视为拥有实体表的列。

Property(x => x.Content, map =>
{
map.Formula("(select 'simple stuff' as 'Content')");
});

// or what you probably want

Property(x => x.Content, map =>
{
map.Formula("(select t.Content FROM AnotherTable t WHERE t.Some_id = id)");
});

关于c# - 在 NHIbernate 中使用带有公式的属性映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13114619/

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