0

我有一个通用的对象类型实体和一些可以与实体相关的类,如EntityDescription和EntityLocation(多对一关系)。抽象中间类的NHibernate(流利)映射

实体类:

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列表仅暴露于某些可以具有位置的特定类(例如Company和Supplier),而不是泛型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; } 

任何帮助表示赞赏。提前致谢。

回答

0

我会说,我们需要的是一个ShouldMap设置。检查这个答案:

FluentNHibernate - Automapping ignore property

所以,我们应该与位置属性的不同的默认处理介绍这个配置:

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

时默认映射使位置属性。下一步,在需要时使用显式映射...