在 Fluent NHibernate 中映射一对一子类

发布于 2024-09-05 11:44:35 字数 339 浏览 4 评论 0原文

我有以下数据库结构:

事件表
ID - 引导 (PK)
名称 - NVarChar
描述 - NVarChar

特殊事件表
ID - 引导 (PK)
开始日期 - 日期时间
EndDate - DateTime

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澜川若宁 2024-09-12 11:44:35

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

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文