2010-06-13 63 views
1

我有以下的數據庫結構:映射一到一個小類

事件表
標識 - 的Guid(PK)
名稱 - 的NVarChar
描述 - 的NVarChar

特殊事件表
Id-Guid(PK)
StartDate - DateTime
EndDate - DateTime

我有一個抽象的Event類和一個從它繼承的SpecialEvent類。最終我會有一個RecurringEvent類,它也會從Event類繼承。如果可能,我想映射SpecialEvent類,同時保留與Ids映射的一對一關係。任何人都可以指出我正確的方向嗎?謝謝!

回答

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