在Fluent NHibernate中映射一对一子类

时间:2010-06-13 23:05:37

标签: fluent-nhibernate nhibernate-mapping

我有以下数据库结构:

活动表
Id - Guid(PK)
名称 - NVarChar
描述 - NVarChar

SpecialEvent表
Id - Guid(PK)
StartDate - DateTime
EndDate - DateTime

我有一个抽象的Event类,以及一个继承它的SpecialEvent类。最终我将有一个RecurringEvent类,它也将继承Event类。我想映射SpecialEvent类,同时保留与ID映射的一对一关系,如果可能的话。任何人都能指出我正确的方向吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我想出了我的问题。这是我的代码:

public abstract class Event : Entity
{
    protected Event() { }
    public Event(string name, DateTime startDate)
    {
        this.Name = name;
        this.StartDate = startDate;
    }

    public virtual string Name { get; private set; }
    public virtual string Description { get; set; }
    public virtual DateTime StartDate { get; protected set; }
    public virtual DateTime? EndDate { get; set; }
}

public class SpecialEvent : Event
{
    protected SpecialEvent() { }
    public SpecialEvent(DateTime startDate, string name) : base(name, startDate) { }
}

public class RecurringEvent : Event
{
    protected RecurringEvent() { }
    public RecurringEvent(string name, DateTime startDate, DateTime baseDate, int recurrenceIntervalDays)
        : base(name, startDate)
    {
        this.RecurrenceIntervalDays = recurrenceIntervalDays;
        this.BaseDate = baseDate;
    }

    public virtual int RecurrenceIntervalDays { get; protected set; }
    public virtual DateTime BaseDate { get; protected set; }
}

public class EventMap : EntityMap<Event>
{
    public EventMap()
    {
        Map(x => x.Name);
        Map(x => x.Description);
        Map(x => x.StartDate);
        Map(x => x.EndDate);
    }
}

public class SpecialEventMap : SubclassMap<SpecialEvent>
{
    public SpecialEventMap()
    {
        KeyColumn("Id");
    }
}

public class RecurringEventMap : SubclassMap<RecurringEvent>
{
    public RecurringEventMap()
    {
        KeyColumn("Id");

        Map(x => x.BaseDate);
        Map(x => x.RecurrenceIntervalDays);
    }
}