【问题标题】:Fluent NHibernate mapping using component使用组件的流畅 NHibernate 映射
【发布时间】:2012-09-19 08:52:32
【问题描述】:

我刚刚开始使用 Fluent NHibernate 开展一个项目。

使用 Fluent NHibernate 映射以下类的正确方法是什么?

public class DurationUnit
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

public class Duration
{
    public virtual int Value { get; set; }
    public virtual DurationUnit Unit { get; set; }
    public virtual int DurationInMinutes { get{ throw new NotImplementedException(); } }
}

public class Event
{
    public virtual int Id { get; set; }
    public virtual String Name { get; set; }
    public virtual Duration MaxDuration { get; set; }
    public virtual Duration MinDuration { get; set; }
}

我最初的方法是为 DurationUnit 和 Event 声明一个 ClassMap,将 Duration 作为 Event 的一个组件。尝试此操作时,我收到了异常:

NHibernate.MappingException:无法确定类型: Entity.DurationUnit

【问题讨论】:

    标签: orm fluent-nhibernate nhibernate-mapping


    【解决方案1】:

    如果你的映射看起来像这样

    public EventMap()
    {
        Component(x => x.MaxDuration, c =>
        {
            c.Map(x => x.Value, "MaxDurationValue");
            c.Reference(x => x.Unit, "MaxDurationUnitId");
        });
    }
    

    然后确保class DurationUnitMap 是公开的并添加到配置中

    【讨论】:

    • 这很好用。我是个白痴,在我的组件映射中使用 Map 而不是 Reference。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2010-11-22
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    相关资源
    最近更新 更多