代码之家  ›  专栏  ›  技术社区  ›  Mike Cole

在Fluent NHibernate中将一个子类映射到一个子类

  •  1
  • Mike Cole  · 技术社区  · 15 年前

    我有以下数据库结构:

    事件表
    ID- Guid(PK)
    姓名- NVarChar
    说明-nvarchar

    特殊事件表
    ID- Guid(PK)
    开始日期-日期时间
    结束日期-日期时间

    我有一个抽象事件类和一个继承自它的特殊事件类。最终,我将拥有一个RecurringEvent类,它也将从事件类继承。如果可能的话,我希望映射SpecialEvent类,同时保留与ID映射的一对一关系。有人能指点我正确的方向吗?谢谢!

    1 回复  |  直到 15 年前
        1
  •  0
  •   Mike Cole    15 年前

    我解决了我的问题。这是我的代码:

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