inheritancenhibernatenhibernate-mapping-by-code

NHibernate mapping by convention inheritance model mapping


I am trying to map this class structure:

public abstract class Entity
{
    public virtual Guid Id {get;set;}
    public virtual int Version {get;set;}
}

public class Parent: Entity
{
    public virtual string ParentName {get;set;}
    ... 
}

public class Child : Parent
{
    public virtual string DisplayName {get;set;}
    ... 
} 

I am doing Mapping by convention mixed with Mapping by code like this:

public class ChildMappingOverride: UnionSubclassMapping<Child>
{

}

public class NHibernateMapping
{
    public void MapDomain()
    {
        ConventionModelMapper mapper = new ConventionModelMapper();
        Type baseEntityType = typeof(Entity);
        mapper.IsEntity((t, declared) => (baseEntityType.IsAssignableFrom(t) && baseEntityType != t) && !t.IsInterface);
        mapper.IsRootEntity((t, declared) => baseEntityType.Equals(t.BaseType));

        mapper.Class<Entity>(map =>
            {
                map.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
                map.Version(x => x.Version, m => 
                {
                    m.Column("Version");
                    m.Generated(VersionGeneration.Never);
                    m.UnsavedValue(0);
                    m.Insert(true);
                    m.Type(new Int32Type());
                    m.Access(Accessor.Property);
                });
            }); 

        mapper.AddMapping<ChildMappingOverride>();
    }
} 

Ok, now when nhibernate creates database schema I get 2 tables instead of only one: Nhibernate created tables: Parent and Child. I want it to create only the table Child that has all the fields: Id, Version, ParentName, DisplayName.

How can I do this? Please help


Solution

  • Mark the Parent class abstract