NHibernate.Linq 计数抛出 NHibernate.QueryException :无法解析属性
我正在使用 S#arp 架构构建一个电子商务网站。 我正在尝试映射类别的层次结构并检索顶级类别。 我为此使用 NHibernate.Linq 。 我有以下实体:
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>();
}
}
具有以下 Fluent 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 of:MyStore.Core.Category”
我做错了什么?
以下是相关问题,但并没有完全解决我的问题:
Fluent nHibernate:需要多对多自引用映射帮助
Fluent NHibernate:ManyToMany 自引用映射< /a>
编辑:
我认为问题在于 Linq 表达式中的 .count 。 这篇文章与此相关,但我不确定如何进展......
I'm building an ecommerce site using S#arp Architecture.
I'm trying to map a hierachy of categories and retrieve the top level categories.
I'm using NHibernate.Linq for this.
I have the following entity:
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>();
}
}
with the following Fluent NHibernate mapping:
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();
}
}
I want to retrieve the root categories. I know I have eight in my db so here's my test:
[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());
}
I figure I just get all Categories where the ParentCategories list is empty (initialized in the ctor of Category). So here's my repository method:
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;
}
When I run my test I get "NHibernate.QueryException : could not resolve property: ParentCategories.Id of: MyStore.Core.Category"
What am I doing wrong?
Here are related questions, but didn't quite solve my problem:
Fluent nHibernate: Need help with ManyToMany Self-referencing mapping
Querying a self referencing join with NHibernate Linq
Fluent NHibernate: ManyToMany Self-referencing mapping
Edit:
I think the problem lies with the .count in the Linq expression. This post related to this, but I'm not sure how to progress...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
知道了。问题出在 linq 表达式中。由于某种原因,它不喜欢 .Count。这可能是 NHibernate.Linq (NH2) 中的一个错误,但我听说 NH3 linq 现在坚如磐石。
无论如何,我的解决方案是使用 Criteria 代替:
varcategories = session.CreateCriteria(typeof (Category))
.Add(Restrictions.IsEmpty("ParentCategories"))
。列表();
感谢 nixsolutions 的 博客文章。 com 让我走上正轨。又全绿了。
Got it. The problem was in the linq expression. It didn't like .Count for some reason. This might be a bug in NHibernate.Linq (NH2), but I hear NH3 linq is rock solid now.
Anyway, my solution is to use a Criteria instead:
var categories = session.CreateCriteria(typeof (Category))
.Add(Restrictions.IsEmpty("ParentCategories"))
.List();
Thanks to a blog post by nixsolutions.com for getting me on the right track. All green again.
您应该使用 Count() 扩展方法而不是 Count 属性。这在 NHibernate 2 中工作得很好。
问候
乔恩
You should be using the Count() extension method instead of the Count property. This works fine in NHibernate 2.
Regards
Jon