将映射字段移至基类时,NHibernate JOIN映射失败

时间:2018-10-03 20:27:57

标签: c# asp.net nhibernate fluent-nhibernate-mapping nhibernate-inheritance

我有两个具有许多公共字段的模型类,所以我决定创建一个基类,并且两个基类都可以从中继承。

地图类已经附带了现有的模型类。

现在子类中继承的所有公共字段都是虚拟的,以保持NHibernate的快乐,并且它们映射都很好,除了一个...

这是我的情况:

public class BaseClass : EntityBase<Guid>
{
   //This is not complaining 
   public virtual string Text { get; set; }

   //This is complaining
   public virtual Guid TouchGuid { get; set; }
}

public class A : BaseClass
{
    //inherited + specific stuff
}

public class B : BaseClass
{
    //inherited + specific stuff
}

现在这些是映射类:

public class AMap : ClassMapping<A>
{
    //Mapping the ID inherited from EntityBase class
    Id(x => x.Id, mapper =>
    {
        mapper.Generator(Generators.GuidComb);
        mapper.Column("Pk_MenuItemId");
    });

    //Mapping the common field inherited, doesn't complain !
    Property(x => x.Mnemonic);

    //This is the one which is complaining, keep in mind it was working
    //before creating the base class and move the TouchGuid property in it.
    Join("Touch", x =>
    {
        x.Key(k =>
        {
            k.Column("EntityId");
            k.ForeignKey("PK_AClassId");
            k.OnDelete(OnDeleteAction.Cascade);
            k.Unique(true);
        });
        x.Property(p => p.TouchGuid);
    });
}

public class BMap : ClassMapping<B>
{
    //Same map as for class A
}

每当我运行该程序,从那些类(表)中加载数据时,都将失败,无法在A表和B表上找到TouchGuid列,这是错误的:

enter image description here

是的,A表和B表之间存在公共数据,但是我无法更改db方案,这会增加太多的复杂性。

我也需要为基类创建一个表吗?如果可能的话,我想避免创建一个新表。

有什么提示可能有问题吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

我相信NHibernate假定具有多个表的数据库模式,因为它默认为隐式多态模式。尝试在映射中设置polymorphism = explicit。

相关问题