简体   繁体   English

当在过滤器中使用时,NHibernate无法解析一对一映射的属性

[英]NHibernate cannot resolve property of one to one mapping when used in a filter

Ok I've got to be doing something really stupid, but I just can't see what it is. 好吧,我做一些非常愚蠢的,但我看不出它是什么。

I have the following query: 我有以下查询:

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

The Recipes model has a property called Metadata which is a one-to-one mapping to the RecipeMetadata model. Recipes模型有一个名为Metadata的属性,它是RecipeMetadata模型的一对一映射。 In other words, every recipe has one and only one RecipeMetadata. 换句话说,每个配方都只有一个RecipeMetadata。 It's mapped like so: 它映射如下:

HasOne(x => x.Metadata);

When I run this query, I get the exception: 当我运行此查询时,我得到异常:

An unhandled exception of type 'NHibernate.QueryException' occurred in NHibernate.dll NHibernate.dll中发生了未处理的“NHibernate.QueryException”类型异常

Additional information: could not resolve property: Metadata.SkillCommon of: KitchenPC.DB.Models.Recipes 附加信息:无法解析属性:Metadata.SkillCommon:KitchenPC.DB.Models.Recipes

If I remove the reference to Metadata : 如果我删除对Metadata的引用:

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

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 . 不仅如此,我可以看到的Metadata的调试器下财产,我可以看到的价值Metadata.SkillCommon So, it is mapped correctly to the database, eagerly loaded (since OneToOne mappings are always eager), and populated with the correct value. 因此,它被正确映射到数据库,急切地加载(因为OneToOne映射总是渴望),并填充正确的值。 Why in the world can't I do a query on it? 为什么在世界上我不能对它进行查询?

It also has nothing to do with SkillCommon (which is a Boolean). 它也与SkillCommon (布尔值)无关。 I also can't filter on other properties within Metadata , such as numeric values. 我也无法过滤Metadata其他属性,例如数值。

Please point out my obviously idiotic error so I can continue on with my evening. 请指出我明显的愚蠢错误,以便我可以继续我的晚上。 Thanks! 谢谢!

Update: 更新:

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. 好像我这样做,因为单个配方总是只有一个RecipeMetadata行,而且没有其他配方会指向同一行,但可能情况并非如此,因为技术上两个配方可以与这个模式共享相同的元数据,我只是通过一个独特的约束阻止它发生。 Thoughts? 思考?

Update 2: 更新2:

I switched to a many-to-one mapping, as suggested by many posts. 我转向多对一映射,正如许多帖子所建议的那样。 In my Recipes mapping I now have: 在我的Recipes映射中,我现在有:

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

And the RecipeMetadata row is joined in and everything works. 并且RecipeMetadata行已加入,一切正常。 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. 所以,这个问题与OneToOne映射无关。

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. 看起来它应该只是工作,但遗憾的是,没有。

You need to call .JoinAlias and explicitly create a JOIN against this entity. 您需要调用.JoinAlias并显式创建针对此实体的JOIN 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);

This appears to work great and generate the desired SQL. 这似乎很有效,并生成所需的SQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM