2010-06-13 66 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); 
    } 
}