问题描述
我有一个场景,我有一个基类作为一个实体,然后是另一个实体,从另一个基类派生。
如果你想避免这种行为,你也许可以看一下。 (我从来没有禁用多态查询能力)。I have a scenario where I have a base class as one entity, then another entity that derives from the other base class. Both have meaning in my domain and can be used separately.
public class MyBaseClass { int ID { get; set; } string Name { get; set; } } public class MyChildClass { string AdditionalField { get; set; } }I have both mapped using Fluent nHibernate using ClassMap like this:
public class MyBaseClassMap : ClassMap<MyBaseClass> { Id("MyBaseClassID"); Map(x => x.Name); } public class MyChildClassMap : SubclassMap<MyChildClass> { Map(x => x.AdditionalField); }What is happening is when I try to fetch a copy of the base class, its using the mapping for the child class. Its as if it doesn't know the the difference between the base and child class, or its choosing the wrong mapping for it. I confirmed this by watching the SQL statement and its joining to the child table and fetching the additional column. Any way to get it to use the right map?
解决方案That's the 'nature' of NHibernate.
The behaviour you're describing, is called 'polymorphic queries'.Since MyChildClass is a MyBaseClass, the MyChildClass instances are retrieved as well.
If you want to avoid this behaviour, you can maybe have a look at the answers in this topic. (I've never 'disabled' the polymorphic query ability).
这篇关于通过nhibernate加载基类错误地使用派生类的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!