在Fluent NHibernate ClassMap公式中引用父表别名? [英] Reference parent table alias in Fluent NHibernate ClassMap Formula?

查看:162
本文介绍了在Fluent NHibernate ClassMap公式中引用父表别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Fluent NHibernate Formula中引用或重命名实体的表别名?我可以通常使用this_,但不幸的是,似乎不一致的基础上NHibernate产生的其他别名的数量。

  this.Map(x => x.IsLocked).Formula(CASE WHEN(SELECT COUNT(*)FROM dbo.Child c WHERE c.InboundDate> = BeginDate AND c.InboundDate< EndDate)> 0 THEN 1 ELSE 0 END); 

当我需要匹配相同名称的字段时, ID。)

  this.Map(x => x.IsLocked).Formula(CASE WHEN(SELECT COUNT (*)FROM dbo.Child c WHERE c.ID = ID)> 0 THEN 1 ELSE 0 END); 

这里,因为 ID c c.ID = ID 将始终为true。在大多数情况下,我可以把它改成 c.ID = this_.ID ,但是在我的一些实体中,NHibernate不会使用这个_ 作为别名,但是像 parent1 _

解决方案<我完全不了解这个问题。公式给出的问题/错误是什么?



注意:NHibernate将在公式中的所有列名前面加上不带别名的实体表的别名注意2:把整个公式包装在parentesis (CASE ... END)中是最好的 / b>

更新:



c.ID = ID将永远评估为真


不,因为第二个ID没有前缀,例如:



pre> Map(u => u.RolesCount).Formula((SELECT COUNT(*)FROM roles r WHERE r.user_id = id));

//得到

SELECT
user0_.Id作为Id0_0_,
(SELECT
COUNT(*)
FROM
角色r
WHERE
r.user_id = user0_.id)作为formula0_0_
FROM
Useruser0_
WHERE
user0_.Id = @ P0;


Is it possible to reference or rename an entity's table alias in a Fluent NHibernate Formula? I can usually use "this_" but that unfortunately does not seem to be consistent based on the number of other aliases NHibernate generates.

this.Map(x => x.IsLocked).Formula("CASE WHEN (SELECT COUNT(*) FROM dbo.Child c WHERE c.InboundDate >= BeginDate AND c.InboundDate < EndDate) > 0 THEN 1 ELSE 0 END");

This gets me by but I run into trouble when I need to match of identically named fields, (like "ID.")

this.Map(x => x.IsLocked).Formula("CASE WHEN (SELECT COUNT(*) FROM dbo.Child c WHERE c.ID = ID) > 0 THEN 1 ELSE 0 END");

Here, because ID is a column in c, c.ID = ID will always evaluate to true. In most cases, I can change this to c.ID = this_.ID to get by, but in some of my entities, NHibernate will not use this_ as an alias, but something like parent1_.

解决方案

i do not understand the question completly. What is the problem/error with Formula presented?

Note: NHibernate will prepend all column names in the Formula without an alias with the alias of the table of the Entity containing the Forumula.

Note2: it is better to wrap the whole formula in parentesis (CASE ... END)

Update:

c.ID = ID will always evaluate to true

no it should not because the second ID has no prefix and will be given the prefix of the entitytable the formula is contained in. try log the sql generated to see.

example:

Map(u => u.RolesCount).Formula("(SELECT COUNT(*) FROM roles r WHERE r.user_id = id)");

//results in

SELECT
    user0_.Id as Id0_0_,
    (SELECT
        COUNT(*)
    FROM
        roles r
    WHERE
        r.user_id = user0_.id) as formula0_0_
FROM
    "User" user0_
WHERE
    user0_.Id=@p0;

这篇关于在Fluent NHibernate ClassMap公式中引用父表别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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