0

我正在使用S#arp Architecture構建電子商務網站。 我試圖映射類別的層次結構並檢索頂層類別。 我爲此使用NHibernate.Linq。 我有以下實體:NHibernate.Linq計數拋出NHibernate.QueryException:無法解析屬性

public class Category : Entity 
{ 
    #region Properties 
    [DomainSignature] 
    [NotNullNotEmpty] 
    public virtual string Name { get; set; } 
    public virtual string Description { get; set; } 
    public virtual int ListOrder { get; set; } 

    public virtual IList<Product> Products { get; set; } 
    public virtual IList<Category> ParentCategories { get; set; } 
    public virtual IList<Category> ChildCategories { get; set; } 
    #endregion 

    public Category() 
    { 
     Products = new List<Product>(); 
     ParentCategories = new List<Category>(); 
     ChildCategories = new List<Category>(); 
    } 
} 

具有以下功能NHibernate映射:

public class CategoryMap : ClassMap<Category> 
{ 
    public CategoryMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.Name); 

     HasManyToMany(p => p.Products) 
      .Cascade.All() 
      .Table("CategoryProduct"); 

     HasManyToMany(c => c.ParentCategories) 
      .Table("CategoryHierarchy") 
      .ParentKeyColumn("Child") 
      .ChildKeyColumn("Parent") 
      .Cascade.SaveUpdate() 
      .AsBag(); 


     HasManyToMany(c => c.ChildCategories) 
      .Table("CategoryHierarchy") 
      .ParentKeyColumn("Parent") 
      .ChildKeyColumn("Child") 
      .Cascade.SaveUpdate() 
      .Inverse() 
      .LazyLoad() 
      .AsBag(); 
    } 
} 

我想要檢索根類別。我知道我在我的數據庫有八個所以這裏是我的測試:

[Test] 
public void Can_get_root_categories() 
{ 
    // Arrange 
    var repository = new CategoryRepository(); 

    // Act 
    var rootCategories = repository.GetRootCategories(); 

    // Assert 
    Assert.IsNotNull(rootCategories); 
    Assert.AreEqual(8, rootCategories.Count()); 
} 

我想我剛剛得到的所有類別,其中ParentCategories列表是空的(在類的構造函數初始化)。因此,這裏是我的倉庫方法:

public IQueryable<Category> GetRootCategories() 
{ 
    var session = NHibernateSession.Current; 

    // using NHibernate.Linq here 
    var categories = from c in session.Linq<Category>() 
        where c.ParentCategories.Count == 0 
        select c; 
    return categories; 
} 

當我運行我的測試,我得到「NHibernate.QueryException:無法解析屬性:ParentCategories.Id的:MyStore.Core.Category」

我在做什麼錯?

這裏有相關的問題,但並沒有完全解決我的問題:

Fluent nHibernate: Need help with ManyToMany Self-referencing mapping

Querying a self referencing join with NHibernate Linq

Fluent NHibernate: ManyToMany Self-referencing mapping

編輯:

我認爲問題出在。在Linq表達式中。 This post與此相關,但我不知道如何進度...

回答

0

明白了。問題出在linq表達式中。它不喜歡。出於某種原因。這可能是NHibernate.Linq(NH2)中的一個錯誤,但是我聽說NH3 linq現在是穩定的。

不管怎樣,我的解決方案是使用代替標準:

變種類別= session.CreateCriteria(typeof運算(類別)) 。新增(Restrictions.IsEmpty( 「ParentCategories」)) 的.List();

感謝blog post by nixsolutions.com讓我走上正軌。所有的綠色再次。

0

您應該使用Count()擴展方法而不是Count屬性。這工作正常NHibernate 2.

關心 喬恩

+1

嗨喬恩。我仍然得到一個與使用相同的錯誤味精失敗:var categories = session.Linq ().Where(c => c.ParentCategories.Count()== 0).ToList(); (...期待夏普拱2 :) – autonomatt 2010-09-30 13:24:57

+0

不適用於我... – Cocowalla 2011-04-25 15:38:50

相關問題