繁体   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