2013-12-20 82 views
2

我內容部分增加了一個一對一的關係:在控制器我做果園查詢加盟實體

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

現在,如果:

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

我已經實現了處理器像這樣:

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和經銷商關係。

我也tryied以這種方式來覆蓋製圖:

public class PersistenceConfiguration : ISessionConfigurationEvents { 

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

似乎沒有任何改變!

我該如何做到這一點?

+0

你能清理代碼嗎?看起來好像有一些錯誤的複製和粘貼:你有AddResellerPart和AddResellerPartPart。 –

+0

對不起,不好剪貼!完成! – danyolgiax

+0

我認爲你需要切換到HQL,並從那裏明確地加入。 –

回答

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

現在我猜...這是正確的和「最簡單的「方式?

+0

這可能是唯一可行的方法,但是您必須使用反射才能訪問私人領域,這很糟糕...請參閱:https://orchard.codeplex.com/discussions/538405 – Piedone