流畅的NHibernate多个抽象类继承。如何为每个具体类创建表而不在db中为抽象类创建表

时间:2017-05-23 04:34:26

标签: c# nhibernate fluent-nhibernate

我有基础抽象类:

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

我有另一个抽象类:

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

    public int? Number { get; set; }

    public int? AdditionalNum { get; set; }

    public DateTime? Date { get; set; }

   ...
}

从这个Document类继承了两个普通的clases:

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

以流利方式映射的类:

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

此方案现在在DB中生成Document表。如何将传入和传出表与所有属性进行映射使用抽象BaseEntityDocument而不在数据库中创建Document表。

1 个答案:

答案 0 :(得分:1)

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

DocumentMap中缺少的是告诉NHibernate Document也是抽象的。我想Fluent有一个类似Abstract()调用的东西,允许指定它。至少这是我们可以使用hbm映射在union-subclass上进行的操作。