使用属性映射在NHibernate的公式 [英] Using a Property mapping with a Formula in NHIbernate

查看:173
本文介绍了使用属性映射在NHibernate的公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个属性映射到另一个表中的任意列。该文件说,该公式可以是任意的SQL,我看到显示出类似的例子。



但是,SQL NHibernate的产生甚至不是有效的。从公式计算出整个SQL语句被注入到 SELECT 语句的中间。

 属性(X => x.Content,地图= GT; 
{
map.Column(内容);
map.Formula(选择简单的东西作为'内容');
});


解决方案

这是这样的公式的设计,它应该是这样的。你需要用你的SQL语句在括号这样可以生成有效的SQL。



此外,你不能指定列和公式在一起。您必须提供整个SQL语句。任何前缀非/躲过将被视为拥有实体的表的列列(在下面的例子中ID)。

 属性(X => x.Content,地图= GT; 
{
map.Formula ((选择简单的东西'作为'内容'));
});

//或者你可能想

房产(X => x.Content,地图= GT;
{
map.Formula( (选择t.Content FROM AnotherTable T其中t.Some_id = ID));
});


I am trying to map a property to an arbitrary column of another table. The docs say that the formula can be arbitrary SQL and the examples I see show similar.

However, the SQL NHibernate generates is not even valid. The entire SQL statement from the formula is being injected into the middle of the SELECT statement.

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

解决方案

This is the way Formula is designed, it is supposed to work that way. You need to wrap your SQL statement in parens so that valid SQL can be generated.

Also, you cannot specify Column and Formula together. You must provide the whole SQL statement. Any non prefixed/escaped columns ('id' in the example below) will be treated as columns of the table of the owning entity.

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

这篇关于使用属性映射在NHibernate的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆