Inheritance 基于另一列的Fluent NHibernate多级继承

Inheritance 基于另一列的Fluent NHibernate多级继承,inheritance,fluent-nhibernate,Inheritance,Fluent Nhibernate,我在为我遇到的问题找到好的文档时遇到问题。我正在重建企业解决方案的一部分,并将NHibernate替换为我们的ORM。我有3个表,我希望它们都相互继承 主帐户:帐户:InvBilling **MasterAccount** id_MasterAccount **Account** id_Account id_MasterAccount id_InvBilling **InvBilling** id_InvBilling id_MasterAccount 对于MasterAccount中的每

我在为我遇到的问题找到好的文档时遇到问题。我正在重建企业解决方案的一部分,并将NHibernate替换为我们的ORM。我有3个表,我希望它们都相互继承

主帐户:帐户:InvBilling

**MasterAccount**
id_MasterAccount

**Account**
id_Account
id_MasterAccount
id_InvBilling

**InvBilling**
id_InvBilling
id_MasterAccount
对于MasterAccount中的每一行,Account中存在一行,其中Id_MasterAccount列相等,IsMaster列为1。但是,Account表中还有其他记录,它们的id_MasterAccounts与父帐户关联。最后,Account表中的每条记录在InvBilling表中都有一条记录

我在设置它们的映射时遇到很多问题。有关于如何设置的帮助吗

我需要以某种方式告诉MasterAccount它应该关联到哪个帐户记录以及它应该使用的列

编辑:以下是我到目前为止的情况:

public class MasterAccountEntity : AccountEntity{
    protected virtual int MAId {get;set;}
}

public class AccountEntity : InvBillingEntity{
    protected virtual int AId {get;set;}
    public virtual MasterAccountEntity MA {get;set;}
    public virtual InvBillingEntity IB {get;set;}
    public virtual bool IsMaster {get;set;}
}

public class InvBilling{
    protected virtual int IBId {get;set;}
    public MasterAccountEntity MA {get;set;}
}
那么对于我的MappingsSome,在如何显示继承方面丢失了什么:

public class FluentHibernateMap : SubclassMap<MasterAccountEntity>{
    Table("MasterAccount");
    Map(x => x.MAId).Column("id_MasterAccount");
}

public class FluentHibernateMap : SubclassMap<AccountEntity>{
    Table("Account");
    Map(x => x.AId).Column("id_Account");
    Map(x => x.IsMaster).Column("isMaster");
    References(x => x.MA).Column("id_MasterAccount").Not.Nullable().Cascade.None();
    References(x => x.IB).Column("id_InvBilling").Not.Nullable().Cascade.None();
}

public class FluentHibernateMap : ClassMap<InvBillingEntity>{
    Table("InvBilling");
    Id(x => x.IBId).Column("id_InvBilling");
    References(x => x.MA).Column("id_MasterAccount").Not.Nullable().Cascade.None();
}

因此,主帐户需要从IsMaster为true的帐户继承记录,然后在InvBilling中继承响应帐户中InvBillingId的记录。

帐户表是一个版本表,它实际上是主表的多面。我将反映这一点,而不是试图在映射中隐藏这一点。那个么,就不可能对历史记录做任何事情,而复杂性也会在不需要的情况下上升

public class Entity
{
    protected virtual int Id { get; private set; }  // should be readonly once set by NHibernate
}

public class AccountMasterEntry : Entity
{
    public virtual Account ActiveAccount { get; private set;}
}

public class Account : Entity
{
    public virtual AccountMasterEntry MA {get;set;}
    public virtual InvBillingEntity IB {get;set;}
    public virtual bool Active {get;set;}
}

public class InvBilling : Entity
{
}

public class AccountMasterEntryMap : ClassMap<AccountMasterEntry>
{
    public AccountMasterEntryMap()
    {
        Id(x => x.Id).GeneratedBy...;

        // optional
        References(x => x.ActiveAccount).Formula("(SELECT acc.id_Account FROM Account acc WHERE id_MasterAccount = acc.id_MasterAccount AND acc.IsMaster = 1)");
    }
}

public class AccountMap : ClassMap<Account>
{
    public AccountMap()
    {
        Id(x => x.Id).GeneratedBy...;

        References(x => x.MA, "id_MasterAccount");
        References(x => x.IB, "id_InvBilling");
        Map(x => x.Active, "ismaster");
    }
    public virtual AccountMasterEntry MA { get; set; }
    public virtual InvBilling IB {get;set;}
    public virtual bool IsMaster {get;set;}
}

public class InvBilling : Entity
{
}

// query
int masterId = ...;
var accountrecord = session.Query<Account>()
    .Where(a => a.MA.Id == masterId && a.Active)  // NHibernate should be smart enough to see that there is no join needed
    .Single();

注意:我不确定invbillingId是否唯一,或者它是否是compositeid

要获得帮助,请发布您的映射/实体代码,您已经添加了我的代码,希望您能提供帮助!这个答案会起作用,这也是我们最终要做的。然而,最初的想法是使用继承,这样您就可以创建一个主记录并通过执行Master.XXX引用其属性。无论如何,我找不到基于IsMaster这样的属性来区分继承的方法,所以我最终做了一些类似于您在答案中所做的事情。你完美地回答了我的评论,但这与这个问题有点出入。我已经研究这个问题好几天了,发了几篇关于它的帖子。我想说的是,你不应该试图用master.XXX来隐藏它,因为泄露抽象的法则会为此困扰你。用公式来区分是可能的,但我强烈反对这样做