参考的实体与功能NHibernate公式 [英] Reference an entity with a formula in Fluent NHibernate
问题描述
我用 N A模式:存储在另一个表,是由一个公式选择1
亲子关系。是否有可能映射该实体使用公式父?
公共类ParentEntity {
公共虚拟INT的ParentId {搞定;组; }
公共虚拟ChildEntity儿童{搞定;组; }
}
公共类ParentMapping:ClassMap< ParentEntity> {
公共ParentMapping(){
表(ParentTable);
标识(X => x.ParentId)。.Column(的ParentId)GeneratedBy.Assigned()Not.Nullable();
参考< ChildEntity>(X => x.Child).Formula(
@(
选择TOP 1 ChildTable.ChildId
FROM ChildTable
,其中ChildTable。的ParentId =的ParentId
)
);
}
}
这是该映射产生这样看起来SQL:
SELECT
this_.ParentId,
this_.ChildEntity_id
FROM ParentTable THIS_
这是不是我要找的。 p>
我怎样才能在父表中引用该子实体和使用,而不是 childID的
,从选择 childID的
公式公式?
的我不会无论如何讨论这种方法的正确性,只是尽量回答。什么你正在尝试做的:应的工作。我检查了公式的正确性测试场景。所以,是的公式可以准确地使用这种方法。
但因为它不工作,我会有点猜测。让我们开始在我的测试情况下,这是工作生成的SQL。
SELECT this_.ParentId作为ParentId3_0_
,( SELECT TOP 1 Child.ChildId
从小孩
其中Child.ParentId = this_.ParentId)作为formula1_0_
从父THIS_
可能出现的问题
我看到了两个可能的问题。
1。
都在你的代码段的第一>引用< ChildEntity>(X => x.Child).Formula(
@(
选择TOP 1 ChildTable.ChildId
FROM ChildTable
其中ChildTable.ParentId =的ParentId
)
都在你的代码段的第一>引用< ChildEntity>(X => x.Child).Formula(
@(
选择TOP 1 ChildTable.ChildId
FROM ChildTable
其中ChildTable.ParentId =的ParentId
)
是子主键的列名: childID的
而在SQL片段是在 ChildEntity_id
SELECT
this_.ParentId,
this_.ChildEntity_id
FROM ParentTable THIS_
2。SQL片段不匹配
其次,你提到的(SQL语句只是以上)是生成什么,但它更像是这个映射的语句:
引用< ChildEntity>(X => ; x.Child).Column(ChildEntity_id)
所以,不可能是有一些较旧的/其他映射,这实际上是在用吗?
摘要
我想说的话,该映射的这种方式工作。那么,你是正确的轨道上,但是的魔鬼隐藏在细节的;)
I have a schema with an N:1
parent-child relationship that is stored in another table and is selected by a formula. Is it possible to map this entity to the parent using a formula?
public class ParentEntity {
public virtual int ParentId { get; set; }
public virtual ChildEntity Child{ get; set; }
}
public class ParentMapping : ClassMap<ParentEntity> {
public ParentMapping() {
Table("ParentTable");
Id(x => x.ParentId).Column("ParentId").GeneratedBy.Assigned().Not.Nullable();
References<ChildEntity>(x => x.Child).Formula(
@"(
SELECT TOP 1 ChildTable.ChildId
FROM ChildTable
WHERE ChildTable.ParentId = ParentId
)"
);
}
}
The SQL that this mapping generates looks like this:
SELECT
this_.ParentId,
this_.ChildEntity_id
FROM ParentTable this_
This is not what I'm looking for.
How can I reference this child entity and use, instead of ChildId
in the parent table, a formula that selects ChildId
from a formula?
I won't anyhow discuss the correctness of this approach, just try to answer. What you are trying to do: should work. I've checked the correctness of the formula in a test scenario. So, yes formula could be used exactly this way.
But because it is not working, I would a bit guess. Let's start with SQL generated in my test case, which is working.
SELECT this_.ParentId as ParentId3_0_
, (SELECT TOP 1 Child.ChildId
FROM Child
WHERE Child.ParentId = this_.ParentId) as formula1_0_
FROM Parent this_
Possible issues
I see two possible issues
1. Different Child ID column names
First of all in your snippet:
References<ChildEntity>(x => x.Child).Formula(
@"(
SELECT TOP 1 ChildTable.ChildId
FROM ChildTable
WHERE ChildTable.ParentId = ParentId
)"
is column name of child primary key: ChildId
while in SQL snippet is the ChildEntity_id
:
SELECT
this_.ParentId,
this_.ChildEntity_id
FROM ParentTable this_
2. SQL Snippet does not match
Secondly, you mentioned that the (SQL Statement just above) is what was generated. But it is more like a statement of this mapping:
References<ChildEntity>(x => x.Child).Column("ChildEntity_id")
So couldn't be there some older/other mapping, which is in fact used?
SUMMARY I wanted to say, that this way of mapping is working. So you are on the correct track, but the devil is hidden in details ;)
这篇关于参考的实体与功能NHibernate公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!