NHibernate映射按约定继承模型映射

时间:2015-10-14 13:41:15

标签: inheritance nhibernate mapping-by-code

我正在尝试映射这个类结构:

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;}
    ... 
} 

我按照惯例进行映射,并按照以下代码进行映射:

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

好的,现在当nhibernate创建数据库模式时,我得到2个表而不是只有一个: Nhibernate创建了表:Parent和Child。 我希望它只创建具有所有字段的表Child:Id,Version,ParentName,DisplayName。

我该怎么做? 请帮忙

1 个答案:

答案 0 :(得分:0)

标记父类摘要