Fluent NHibernate 多重抽象 class 继承。如何在没有为抽象 class 的数据库中创建 table 的情况下为每个具体 class 创建 table

Fluent NHibernate multiple abstract class inheritance. How create table per concrete class without create table in db for abstract class

我有基础摘要class:

public abstract class BaseEntity : IBaseEntity
{
    protected BaseEntity()
    {
    }

    public Guid Id { get; set; }

    public User CreatedBy { get; set; }

    public DateTime CreatedDate { get; set; }

    public User LastChangedBy { get; set; }

    public DateTime LastChangedDate { get; set; }

    public bool Removed { get; set; }

    public string Uid { get; }
}

我还有一个摘要class:

public abstract class Document : BaseEntity, IDocument
{
    public Document()
    {
    }

    public int? Number { get; set; }

    public int? AdditionalNum { get; set; }

    public DateTime? Date { get; set; }

   ...
}

从此文档class继承了两个普通类:

public class Incoming : Document
{
}
public class Outgoing : Document
{
}

类 像这样映射成 fluent:

public class BaseEntityMap : ClassMap<BaseEntity>
{
    public BaseEntityMap()
    {
        Id(x => x.Id).GeneratedBy.Assigned();

        Map(x => x.CreatedDate);
        Map(x => x.LastChangedDate);
        Map(x => x.Removed);
        Map(x => x.Uid);

        References(x => x.CreatedBy).Cascade.SaveUpdate();
        References(x => x.LastChangedBy).Cascade.SaveUpdate();

        UseUnionSubclassForInheritanceMapping();
    }
}

public class DocumentMap : SubclassMap<Document>
{
    public DocumentMap()
    {
        Map(x => x.Number);
        Map(x => x.AdditionalNum);
        Map(x => x.Header);
        Map(x => x.Summary);
        Map(x => x.FullNumber);
        Map(x => x.Archive);
        Map(x => x.CaseDate);
        Map(x => x.CloseMark);

        References(x => x.NewFirstPage).Cascade.SaveUpdate();
        References(x => x.NewSealPage).Cascade.SaveUpdate();
        References(x => x.Type).Cascade.SaveUpdate();
        References(x => x.Stage).Cascade.SaveUpdate();
        References(x => x.Case).Cascade.SaveUpdate();

        HasMany(x => x.Attaches).Inverse();
    }
}

public class IncomingMap : SubclassMap<Incoming>
{
    public IncomingMap()
    {
        Extends(typeof(Document));
        Table("tbl_Incoming");

        Map(x => x.Resolution);
        Map(x => x.ResolutionDate);
        Map(x => x.OrganizationOutgoingNumber);
        Map(x => x.OrganizationOutgoingDate);
        Map(x => x.IncomingDocumentType);

        References(x => x.From).Cascade.SaveUpdate();
        References(x => x.ResolutionEmployee).Cascade.SaveUpdate();
    }
}

public class OutgoingMap : SubclassMap<Outgoing>
{
    public OutgoingMap()
    {
        Extends(typeof(Document));
        Table("tbl_Outgoing");

        Map(x => x.DivisionNumber);
        Map(x => x.OrganizationIncomingNumber);
        Map(x => x.OrganizationIncomingDate);
        Map(x => x.OrganizationResolution);
        Map(x => x.Reserved);
        Map(x => x.AgreementNeed);
        Map(x => x.AgreementStatus);

        References(x => x.Author).Cascade.SaveUpdate();

        HasMany(x => x.OutgoingAdresses).Inverse();
        HasMany(x => x.SignedEmployees).Inverse();
    }
}

此方案现在在数据库中生成文档 table。如何将传入和传出 table 映射到所有属性 prom abstract BaseEntityDocument 而无需在数据库中创建 Document table。

在您的 BaseEntityMap 中,您使用 UseUnionSubclassForInheritanceMapping。我不习惯 Fluent,但它似乎在幕后做了两件事:

您的 DocumentMap 中缺少的是告诉 NHibernate Document 也是抽象的。我想 Fluent 有类似 Abstract() 的调用,允许指定它。至少那是我们可以在 union-subclass 上用 hbm 映射做的。