gpt4 book ai didi

c# - Orchard 查询加入的实体

转载 作者:行者123 更新时间:2023-11-30 22:10:39 26 4
gpt4 key购买 nike

我有一个内容部分添加了一对一的关系:

public class AddResellerPart : ContentPart<AddResellerPartRecord>
{

private readonly LazyField<ResellerPart> _reseller = new LazyField<ResellerPart>();

public LazyField<ResellerPart> ResellerField { get { return _reseller; } }

public ResellerPart Reseller
{
get { return _reseller.Value; }
set { _reseller.Value = value; }
}
...

我已经实现了处理程序:

    OnInitializing<AddResellerPart>(PropertySetHandlers);
OnLoaded<AddResellerPart>(LazyLoadHandlers);
...

现在如果在 Controller 中我做这样的事情:

var customer = _orchardServices.ContentManager
.Query<AddResellerPart, AddResellerPartRecord>()
.List().First();

var name = customer.Reseller.Description;

它就像一个魅力!但如果我尝试这样做:

var customer = _orchardServices.ContentManager
.Query<AddResellerPart, AddResellerPartRecord>()
.Where(x => x.Reseller.Description.Contains(filterDescription))
.List().First();

我得到错误:

"could not resolve property: Reseller.Descriptionof:
myproject.Core.Models.AddResellerPartRecord"

我认为这与实体之间的连接由处理程序管理并且 NHibernate 对 AddResellerPart 和 Reseller 关系一无所知这一事实有关。

我也尝试以这种方式覆盖映射:

public class PersistenceConfiguration : ISessionConfigurationEvents {

public void Created(FluentConfiguration cfg, AutoPersistenceModel defaultModel) {
defaultModel.Override<AddResellerPart>(mapping => mapping.References(x => x.Reseller, "Reseller_Id"));
}

似乎什么都没有改变!

我怎样才能做到这一点?

最佳答案

几个小时后,我感觉自己像个巫师,把 bat 翅膀扔进了大锅里,我设法让它像这样工作:

var query =_orchardServices.ContentManager.HqlQuery()
.Join(alias=>alias.ContentPartRecord<AddResellerPartRecord>());

var defaultHqlQuery = query as DefaultHqlQuery;
var fiJoins = typeof(DefaultHqlQuery).GetField("_joins", BindingFlags.Instance | BindingFlags.NonPublic);
var joins = fiJoins.GetValue(defaultHqlQuery) as List<Tuple<IAlias, Join>>;
joins.Add(new Tuple<IAlias, Join>(new Alias("myproject.Core.Models"), new Join("ResellerPartRecord", "ResellerAlias", ",")));
Action<IHqlExpressionFactory> joinOn = predicate => predicate.EqProperty("Id", "addResellerPartRecord.Reseller");
query = query.Where(alias => alias.Named("ResellerAlias"), joinOn);

query = query.Where(alias => alias.Named("ResellerAlias"), predicate => predicate.Like("Description", filterDescription, HqlMatchMode.Anywhere));
var users = query.List();

现在我想...这是正确且“最简单”的方法吗?

关于c# - Orchard 查询加入的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20713066/

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