簡體   English   中英

NHibernate(Fluent)映射抽象了一個中間類

[英]NHibernate (Fluent) mapping abstracting an intermediate class

我有一個通用的對象類型Entity和一些可以與Entity相關的類,例如EntityDescription和EntityLocation(多對一關系)。

實體類:

public class Entity
{
    public virtual Int64 ID { get; set; }
    // and other properties
}

EntityDescription類:

public class EntityDescription 
{
    public virtual Int64 ID { get; set; }
    public virtual Entity Entity { get; set; }
    public virtual String Description { get; set; }
    // and other properties
}

EntityLocation類:

public class EntityLocation 
{
    public virtual Int64 ID { get; set; }
    public virtual Entity Entity { get; set; }
    public virtual String Location { get; set; }
    // and other properties
}

有許多更具體的實體類型,例如公司,供應商,雇員等。為了使事情變得更加有趣,EntityDescription適用於所有特定類型,但是只能為某些類型分配位置(EntityLocation僅適用於某些類型)。

如何在Fluent-NHibernate中映射這些類,以使EntityLocation列表僅在某些可以具有位置的特定類(例如,公司和供應商)上公開,而不在通用Entity類上公開?

// This property can exist in Entity
public virtual IList<EntityDescription> Descriptions { get; set; }

// I want this property to only exist in some specific types, not in Entity
public virtual IList<EntityLocation > Locations { get; set; }

任何幫助表示贊賞。 提前致謝。

我要說的是,我們需要的是ShouldMap設置。 檢查以下答案:

FluentNHibernate-自動映射忽略屬性

因此,我們應該以不同的Locations屬性默認處理方式引入此配置:

public class AutoMapConfiguration : DefaultAutomappingConfiguration
{
    private static readonly IList<string> IgnoredMembers = 
                        new List<string> { "Locations"}; // ... could be more...

    public override bool ShouldMap(Member member)
    {
        var shouldNotMap = IgnoredMembers.Contains(member.Name);

        return base.ShouldMap(member) && !shouldNotMap;
    }
}

默認情況下,這將使Locations屬性被映射。 下一步是在需要的地方使用顯式映射...

暫無
暫無

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

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