流利的NHibernate - IndexOutOfRange

Car*_*ema 5 nhibernate fluent-nhibernate nhibernate-3 fluent-nhibernate-mapping

我已阅读所有帖子并知道IndexOutOfRange通常会发生,因为列被引用了两次.但我根据我的映射看不出这是怎么回事.如果配置中的SHOW_SQL为true,我会看到Events表中的Insert ,然后IndexOutOfRangeExceptionRadioButtonQuestions表引用的表.我看不到它试图使用的SQL会产生异常.我尝试使用AutoMapping,现在已经切换到ClassMap这两个类的完全,以尝试缩小问题范围.

public class RadioButtonQuestion : Entity
{
    [Required]
    public virtual Event Event { get; protected internal set; }

    [Required]
    public virtual string GroupIntroText { get; set; }
}

public class Event : Entity
{
    [Required]
    public virtual string Title { get; set; }

    [Required]
    public virtual DateTime EventDate { get; set; }

    public virtual IList<RadioButtonQuestions> RadioButtonQuestions { get; protected internal set; }
}




public class RadioButtonQuestionMap : ClassMap<RadioButtonQuestion>
{
    public RadioButtonQuestionMap()
    {
        Table("RadioButtonQuestions");

        Id(x => x.Id).Column("RadioButtonQuestionId").GeneratedBy.Identity();

        Map(x => x.GroupIntroText);
        References(x => x.Event).Not.Nullable();
    }
}


public class EventMap : ClassMap<Event>
{
    public EventMap()
    {
        Id(x => x.Id).Column("EventId").GeneratedBy.Identity();
        Map(x => x.EventDate);
        Map(x => x.Title);
        HasMany(x => x.RadioButtonQuestions).AsList(x => x.Column("ListIndex")).KeyColumn("EventId").Not.Inverse().Cascade.AllDeleteOrphan().Not.KeyNullable();
    }
}
Run Code Online (Sandbox Code Playgroud)

生成的SQL看起来正确:

create table Events (
    EventId INT IDENTITY NOT NULL,
   EventDate DATETIME not null,
   Title NVARCHAR(255) not null,
   primary key (EventId)
)

create table RadioButtonQuestions (
    RadioButtonQuestionId INT IDENTITY NOT NULL,
   GroupIntroText NVARCHAR(255) not null,
   EventId INT not null,
   ListIndex INT null,
   primary key (RadioButtonQuestionId)
)
Run Code Online (Sandbox Code Playgroud)

这是使用NH 3.3.0.4000和FNH 1.3.0.727.当我尝试保存一个新事件(附带一个RadioButtonQuestion)时,我明白了

NHibernate:INSERT INTO事件(EventDate,Title)VALUES(@ p0,@ p1); @ p0 = 5/21/2012 12:32:11 PM [类型:DateTime(0)],@ p1 ='我的测试事件' [类型:字符串(0)] NHibernate:选择@@ IDENTITY

Events.Tests.Events.Tasks.EventTasksTests.CanCreateEvent:NHibernate.PropertyValueException:用于Events.Domain.RadioButtonQuestion._Events.Domain.Event.RadioButtonQuestionsIndexBackref错误脱水属性值----> System.IndexOutOfRangeException:与ParameterIndex '3' 的SqlCeParameter此SqlCeParameterCollection不包含此内容.

因此,如果一个列真的被引用两次,那么我的FNH配置导致该行为的问题是什么?我正在尝试订购双向关系(一个事件有许多单选按钮问题)(我会维护它,因为NH不会与我读过的双向关系).FWIW我也通过删除Eventfrom来尝试将其作为单向关系RadioButtonQuestion,它仍然导致相同的异常.

小智 7

我在代码(NH 3.3.1)中使用映射,我注意到添加Update(false)和Insert(false)可以解决问题:

ManyToOne(x => x.DictionaryEntity, map =>
{
    map.Column("Dictionary");
    map.Update(false);
    map.Insert(false);
    map.Cascade(Cascade.None);
    map.Fetch(FetchKind.Select);
    map.NotFound(NotFoundMode.Exception);
    map.Lazy(LazyRelation.Proxy);
});
Run Code Online (Sandbox Code Playgroud)


小智 5

您具有双向关联,因此一侧应标记为Inverse(),并且只能是RadioButtonQuestions集合。如果您希望集合成为所有者,则必须在RadioButtonQuestion类中删除对该事件的引用。

此外,表RadioButtonQuestions中的EventId列不可为空,如果集合映射不是逆的,则可能导致问题。请参阅文档中的注释