Fluent NHibernate: override derived classes not in the base class auto-mapping

杀马特。学长 韩版系。学妹 提交于 2019-12-07 16:41:10

问题


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?


回答1:


Your override is telling FNH that you will manually write the mappings for that class. The error you are getting is because there is nothing being mapped for Organisation (if you look at the generated HBM.xml it will be empty).

What exactly are you wanting to write the override for?

Edit:

In that case, you can do something like this:

public class MyAlteration : IAutoMappingAlteration
{
    public void Alter(AutoPersistenceModel model)
    {
        model.ForTypesThatDeriveFrom<User>(
            map => map.HasMany<User>( x => x.Children)
        );
    }       
}

And when configuring fluent nhibernate:

model.Alteration( a => a.Add<MyAlteration>());

Note: This is using the latest codebase of fluent nhibernate (1.0RC).




回答2:


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.



来源:https://stackoverflow.com/questions/1226723/fluent-nhibernate-override-derived-classes-not-in-the-base-class-auto-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!