在过滤器中使用时的NHibernate无法解析一个属性一一映射属性、器中、NHibernate

由网友(星川瑶)分享简介:好,我的有的做一些非常愚蠢的,但我看不出它是什么。Ok I've got to be doing something really stupid, but I just can't see what it is.我有以下查询:Recipes recipe = null;var q = session.Query...

好,我的有的做一些非常愚蠢的,但我看不出它是什么。

Ok I've got to be doing something really stupid, but I just can't see what it is.

我有以下查询:

Recipes recipe = null;
var q = session.QueryOver<Recipes>(() => recipe)
   .Where(p => p.Metadata.SkillCommon)
   .Where(p => !p.Hidden);

食谱模型有一个名为属性,它是一个一对一的映射到 RecipeMetadata 模式。换句话说,每一个配方有一个且只有一个RecipeMetadata。它映射像这样:

The Recipes model has a property called Metadata which is a one-to-one mapping to the RecipeMetadata model. In other words, every recipe has one and only one RecipeMetadata. It's mapped like so:

HasOne(x => x.Metadata);

当我运行此查询,我得到异常:

When I run this query, I get the exception:

类型的未处理的异常'NHibernate.QueryException'的发生   NHibernate.dll

An unhandled exception of type 'NHibernate.QueryException' occurred in NHibernate.dll

信息:无法解析属性:中Metadata.SkillCommon:KitchenPC.DB.Models.Recipes

Additional information: could not resolve property: Metadata.SkillCommon of: KitchenPC.DB.Models.Recipes

如果我删除提及

var q = session.QueryOver<Recipes>(() => recipe)
   //.Where(p => p.Metadata.SkillCommon)
   .Where(p => !p.Hidden);

查询工作正常。不仅如此,我可以看到在调试器下财产,我可以看到 Metadata.SkillCommon 的价值。因此,它被正确映射到数据库,即时加载(因为一对一的映射总是渴望),并填入正确的值。为什么在世界上我不能做一个查询就可以了?

The query works fine. Not only that, I can see the Metadata property under the debugger and I can see the value of Metadata.SkillCommon. So, it is mapped correctly to the database, eagerly loaded (since OneToOne mappings are always eager), and populated with the correct value. Why in the world can't I do a query on it?

它也有无关 SkillCommon (这是一个布尔值)。我也不能在,对其他属性进行过滤,如数值。

It also has nothing to do with SkillCommon (which is a Boolean). I also can't filter on other properties within Metadata, such as numeric values.

请指出我的明显愚蠢的错误,所以我可以继续我的夜晚。谢谢!

Please point out my obviously idiotic error so I can continue on with my evening. Thanks!

更新:

在阅读这篇文章< /一>,我开始以为我不真的有一个一对一的映射。它的似乎的像我这样做,是因为一个单一的配方总是有一个RecipeMetadata行,并没有其他的配方将指向同一行,但也许这是不是这样的,因为在技术上两个配方的可以的共享与此架构相同的元数据,我只需$发生过的唯一约束p $ pvent它。思考?

After reading this article, I'm beginning to think I don't really have a One-To-One mapping. It seems like I do, because a single recipe will always have a single RecipeMetadata row, and no other Recipe will point to that same row, but perhaps this is not the case, since technically two recipes could share the same metadata with this schema, I just prevent it from happening through a unique constraint. Thoughts?

更新2:

我切换到一个多到一的映射,所建议的很多帖子。在我的食谱映射我现在有:

I switched to a many-to-one mapping, as suggested by many posts. In my Recipes mapping I now have:

References(x => x.Metadata).Column("RecipeId");

我现在可以运行查询:

I can now run the query:

var q = session.QueryOver<Recipes>(() => recipe)
   .Fetch(prop => prop.Metadata).Eager()
   .Where(p => !p.Hidden);

RecipeMetadata 行加入,一切工作。但是,的还是的时候我补充一下:

And the RecipeMetadata row is joined in and everything works. However, still when I add:

.Where(p => p.Metadata.SkillCommon)

我如上得到相同的异常。所以,这个问题已经没有任何关系一对一的映射。

I get the same exception as above. So, this problem had nothing to do with OneToOne mappings.

推荐答案

想通了这一点,虽然我不知道这是最好的和/或只有做到这样。现在看来似乎应该的只是工作的,但遗憾的是,没有。

Figured this out, though I'm not sure if this is the best and/or only way to do this. It seems like it should just work but sadly, no.

您需要调用 .JoinAlias​​ 并明确创建一个加入对这个实体。然后,您可以参考该加盟以后:

You need to call .JoinAlias and explicitly create a JOIN against this entity. You can then refer to that join later on:

Models.RecipeMetadata metadata = null;
var q = session.QueryOver<Recipes>(() => recipe)
   .JoinAlias(r => r.Metadata, () => metadata)
   .Where(() => metadata.SkillCommon)
   .Where(p => !p.Hidden);

这似乎工作的伟大,并生成所需的SQL。

This appears to work great and generate the desired SQL.