簡體   English   中英

當映射的字段移動到基類時,NHibernate JOIN 映射失敗

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

有兩個具有許多公共字段的模型類,我決定創建一個基類,並且它們都繼承它。

現有的模型類已經帶有地圖類。

現在在子類中繼承的所有公共字段都是虛擬的,以使 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 列,這是錯誤:

在此處輸入圖片說明

是的,A 表和 B 表之間有共同的數據,但我不能改變 db 方案,它現在會增加太多的復雜性。

我是否也需要為基類創建一個表? 如果可能,我想避免創建一個新表。

任何可能出問題的提示?

謝謝 !

我相信 NHibernate 假設一個包含多個表的 DB 模式,因為它默認為隱式多態模式。 嘗試在映射中設置 polymorphism=explicit。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM