gpt4 book ai didi

c# - nhibernate joinqueryover 不相关的表(相同的外键)

转载 作者:行者123 更新时间:2023-11-30 17:56:48 25 4
gpt4 key购买 nike

基本上我想做的是用外键连接两个表。我有这个查询:

        var result =
_session.QueryOver(() => contentReferenceAlias)
.Inner.JoinAlias(() => contentReferenceAlias.ContentReference, () => contentLibrarySearchAlias.ContentReference)
.Where(() => contentReferenceAlias.ToLanguage.Id == languageId && contentReferenceAlias.ContentReference.Id == contentLibrarySearchAlias.ContentReference.Id)
.SelectList(list => list
.Select(() => contentReferenceAlias.ContentReference)
.Select(() => contentLibrarySearchAlias.ContentReference)
.Select(() => contentReferenceAlias.ContentReference.Id).WithAlias(() => resultAlias.ContentReferenceId)
.Select(() => contentReferenceAlias.ContentReference.Id).WithAlias(() => resultAlias.ContentReferenceId)
.Select(() => contentReferenceAlias.OrderedFrom).WithAlia

我正在尝试重新创建的 SQL:

  SELECT A.OrderedFrom, C.LastOrdered, A.ContentReferenceId, B.Title FROM TranslationContentReference A
INNER JOIN TranslationOrder C ON (A.TranslationOrderId = C.Id)
INNER JOIN ContentLibrarySearch B ON (A.ContentReferenceId = b.ContentReferenceId)
WHERE A.ToLanguageId = 'xxxx-xxxx-xxxx-xxxx-xxxx'

最佳答案

如果我正确理解您的场景,则无法通过 QueryOver API 实现中间人(外键引用)加入。 NHibernate 需要知道一路向下的路径,所以如果没有从 TranslationContentReferenceContentReferenceContentLibrarySearch 的显式映射,那么我们就无法创建正确的加入别名。

所以,第一个选择是扩展中间人对象

public class ContentReference
{
...
public virtual IList<TranslationContentReference> TranslationContentReference { get; set;}
public virtual IList<ContentLibrarySearch> ContentLibrarySearch { get; set;}
}

然后我们可以导航(创建路径)

  • TranslationContentReferenceContentReference
  • ContentReferenceContentLibrarySearch

第二个选项是创建 ISQLQuery

ISQLQuery query = session.CreateSQLQuery(
"SELECT A.OrderedFrom, C.LastOrdered, A.ContentReferenceId, B.Title
FROM TranslationContentReference A
INNER JOIN TranslationOrder C ON (A.TranslationOrderId = C.Id)
INNER JOIN ContentLibrarySearch B ON (A.ContentReferenceId = b.ContentReferenceId)
WHERE A.ToLanguageId = :language");
query.SetString("language", "xxxx-xxxx-xxxx-xxxx-xxxx");
var result = query.SetResultTransformer(new AliasToBeanResultTransformer(typeof(MyDTO)))
.List();

关于c# - nhibernate joinqueryover 不相关的表(相同的外键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804204/

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