流利的NHibernate:覆盖不在基类自动映射中的派生类 [英] Fluent NHibernate: override derived classes not in the base class auto-mapping
问题描述
故事:
我有class User和class Organization:User。我没有使用这些类的映射,让FNH自动映射。然后,我添加了
public class OrganizationMap:IAutoMappingOverride< Organization>
public void Override(AutoMap< Organization> mapping)
{
}
}
请注意,没有覆盖。所以我没有想到FNH的行为有任何变化。但是我得到了这个(在架构导出期间):
lockquote
NHibernate.MappingException:
(XmlDocument)(2,4): XML验证
错误:
命名空间中的元素'class'urn:nhibernate-mapping-2.2'
包含不完整的内容。 $ meta $ b $ subsection,cache,synchronize,
comment,tuplizer,id,composite-id'
名字空间
'urn: nhibernate-mapping-2.2'。
生成的Orders.Core.Organization.hbm.xml真的是空的:
< hibernate-mapping xmlns =urn:nhibernate-mapping-2.2default-access =>
< class name =Orders.Core.Organization,Orders.Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = nulltable =Organizationsxmlns =urn:nhibernate-mapping-2.2 />
< / hibernate-mapping>
所以,在回顾User.hbm之后,我得到了这个想法 - 我需要重写Organization基类如下:
public class UserMap:IAutoMappingOverride< User>
public void Override(AutoMap< User> mapping)
{
mapping.JoinedSubClass< Organization>(ColumnId,m => {...}
);
$ b 但是,我最好在单独的映射覆盖类为组织...毕竟,如果我有5个子类全部在单个覆盖方法映射成为什么。
这是可能的吗?
解决方案 (RC之后的一些修改)现在是可能的。我怀疑这是不是因为我问了;)
所以我有这个
{
m.HasMany(x => x.Currencies).Element(Currency)。AsBag );
}
);
并且在升级到RC后停止工作。然后我把它移到它自己的类中。
pre $ public $ OrganizationMap:IAutoMappingOverride< Organization>
{
public void Override(AutoMapping< Organization> mapping)
{
mapping.HasMany(x => x.Currencies).Element(Currency)。AsBag );
}
}
它重新开始工作。就像我想要的一样!现在我甚至不需要指定JoinedSubClass,因为这是默认的。我可以重写我的子类属性,这很酷。
虽然不容易弄清楚为什么NH开始抱怨字符串的关联...我甚至认为RC中的元素被打破了。我不知道为什么JoinedSubClass仍然有这个映射部分,如果它不完全工作。
The story: I had class User and class Organization: User. I did not use any mappings for these classes, let FNH do mapping automatically. Then, I added
public class OrganizationMap : IAutoMappingOverride<Organization>
{
public void Override(AutoMap<Organization> mapping)
{
}
}
Notice there're no overrides. So I did not expect any changes in FNH behavior. But I got this (during schema export actually):
NHibernate.MappingException: (XmlDocument)(2,4): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has incomplete content. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'.
The generated Orders.Core.Organization.hbm.xml was really empty:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
<class name="Orders.Core.Organization, Orders.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Organizations" xmlns="urn:nhibernate-mapping-2.2" />
</hibernate-mapping>
So, after I reviewed the User.hbm I got the idea - I need to override Organization in the base class like this:
public class UserMap : IAutoMappingOverride<User>
{
public void Override(AutoMap<User> mapping)
{
mapping.JoinedSubClass<Organization>("ColumnId", m => {...}
);
}
}
But, I would better like to do this in a separate mapping override class for Organization... after all, what would the mapping become if I have 5 subclasses all in single Override method.
Is this possible?
Turned out that with latest FNH (some revision after RC) this is possible now. I wonder if this is because I asked ;-)
So I had this
mapping.JoinedSubClass<Organization>("UserId", m =>
{
m.HasMany(x => x.Currencies).Element("Currency").AsBag();
}
);
and it stopped working after upgrading to RC. Then I moved this into its own class
public class OrganizationMap : IAutoMappingOverride<Organization>
{
public void Override(AutoMapping<Organization> mapping)
{
mapping.HasMany(x => x.Currencies).Element("Currency").AsBag();
}
}
it started to work again. Just like I wanted! Now I don't even need to indicate JoinedSubClass as this is the default, anyway. I can just override my subclass properties which is cool.
Though it wasn't too easy to figure out why NH started to complain about association of strings... I even thought that .Element is broken in RC. I wonder why JoinedSubClass still has this mapping part if it doesn't completely work.
这篇关于流利的NHibernate:覆盖不在基类自动映射中的派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!