Orchard查询加入实体

时间:2013-12-20 22:39:56

标签: c# orchardcms

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

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);
    ...

现在如果在控制器中我做了这样的事情:

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

似乎没有任何改变!

我该如何做到这一点?

1 个答案:

答案 0 :(得分:3)

在我感觉自己像是一个将蝙蝠翅膀投入大锅的巫师的几个小时之后,我设法让这一切都像这样:

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();

现在我猜...这是正确的,“最简单”的方式吗?