views:

11

answers:

1

I have the following database structure:

Event table
Id - Guid (PK)
Name - NVarChar
Description - NVarChar

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

I have an abstract Event class, and a SpecialEvent class that inherits from it. Eventually I will have a RecurringEvent class which will inherit from the Event class also. I'd like to map the SpecialEvent class while preserving a one-to-one relationship mapped with the Ids, if possible. Can anybody point me in the correct direction? Thanks!

A: 

I figured my problem out. Here is my code:

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);
    }
}
Mike C.

related questions