2

我有一个 FluentNHibernate 的 EntityBase 类:

public abstract class EntityBase<T>
{
    public EntityBase()
    {

    }

    public static T GetById(int id)
    {
        return (T)Hibernate.Session.Get<T>(id);
    }
    public virtual void Save()
    {
        using (var transaction = Hibernate.Session.BeginTransaction())
        {
            Hibernate.Session.SaveOrUpdate(this);
            transaction.Commit();
        }
    }
    public static IList<T> List()
    {
        return Hibernate.Session.CreateCriteria(typeof(T)).List<T>();
    }
    public static IList<T> ListTop(int i)
    {
        return Hibernate.Session.CreateCriteria(typeof(T)).SetMaxResults(i).List<T>();
    }
    public virtual void Delete()
    {
        using (var transaction = Hibernate.Session.BeginTransaction())
        {
            Hibernate.Session.Delete(this);
            transaction.Commit();
        }
    }
}

我有一个基成员类,也是数据库中的一个表:

abstract public class BaseMember:EntityBase<BaseMember>
{
    public virtual int Id { get; set; }
    public virtual string Email { get; set; }
    public virtual string Password { get; set; }
    public virtual string RecordDate { get; protected set; }

    public BaseMember()
    {

    }
}

我有另一个派生自 BaseMember 类的成员类:

public class IndividualMember : BaseMember
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string PhoneNumber { get; set; }
    public virtual string MobilePhoneNumber { get; set; }
    public virtual DateTime BirthDate { get; set; }
    public virtual bool Gender { get; set; }
    public virtual string ProfileImage { get; set; }
    public virtual string AddressDefinition { get; set; }
    public virtual string ZipCode { get; set; }
    public virtual DateTime RecordDate { get; set; }

    public IndividualMember()
    {

    }
}

如何将这些类与数据库中的 BaseMember 和 IndividualMember 表映射?

4

2 回答 2

0

See here: https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping

于 2012-10-16T11:56:06.923 回答
0

Fluent NHibernate 中有不同类型的继承映射策略。您可以对派生类使用 SubclassMap 映射。

策略:每类层次结构表、每子类表和每具体类表。

对于 table-per-class-hierarchy 策略,您只需要指定鉴别器列。

更多参考:

http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping#wiki-subclasses

于 2013-04-29T12:57:59.230 回答