简体   繁体   English

当映射的字段移动到基类时,NHibernate JOIN 映射失败

[英]NHibernate JOIN mapping fails when the mapped field is moved to a base class

Having two model classes with many common fields, I decided to create a base class and both of them to inherit from it.有两个具有许多公共字段的模型类,我决定创建一个基类,并且它们都继承它。

The existent model classes already came with map classes.现有的模型类已经带有地图类。

All the common fields which are now inherited in the child classes are virtual to keep NHibernate happy and they are all mapping alright, except for one...现在在子类中继承的所有公共字段都是虚拟的,以使 NHibernate 开心,并且它们都映射正常,除了一个......

This my case:这是我的情况:

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
}

Now these are the mapping classes:现在这些是映射类:

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
}

Whenever I run the program, loading the data from those classes (tables), will fail saying it couldn't find the TouchGuid column on the A table respectively B table, here is the error:每当我运行程序时,从这些类(表)加载数据时,都会失败,说在 A 表和 B 表上找不到 TouchGuid 列,这是错误:

在此处输入图片说明

Yes, there is common data between A and B tables but I can't change the db scheme, it would add too much complexity now.是的,A 表和 B 表之间有共同的数据,但我不能改变 db 方案,它现在会增加太多的复杂性。

Do I need to create a table for the base class too ?我是否也需要为基类创建一个表? I would like to avoid created a new table if possible.如果可能,我想避免创建一个新表。

Any hints of what could be wrong ?任何可能出问题的提示?

Thank you !谢谢 !

I believe NHibernate assumes a DB schema with multiple tables because it defaults to implicit polymorphism mode.我相信 NHibernate 假设一个包含多个表的 DB 模式,因为它默认为隐式多态模式。 Try setting polymorphism=explicit in the mappings.尝试在映射中设置 polymorphism=explicit。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM