我读过所有的帖子,并知道IndexOutOfRange通常发生,因为列被引用两次。但是我根据我的映射看不到这是怎么回事。在配置中SHOW_SQL为true,我看到Events
表中的插入,然后引用RadioButtonQuestions
表。我看不到它正在尝试使用的SQL生成异常。我尝试使用AutoMapping,现在已经切换到完整ClassMap
这两个类来尝试缩小问题范围。流利NHibernate的 - IndexOutOfRange
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();
}
}
生成的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)
)
这是用NH 3.3.0.4000和FNH 1.3.0.727。当我尝试保存一个新事件时(连同一个RadioButtonQuestion),我看到
NHibernate:INSERT INTO事件(EventDate,Title)VALUES(@ p0,@ p1); @ p0 = 2012/5/21 12:32 :11 PM [类型:日期时间(0)],@ P1 = '我的测试事件'[类型:String(0)] NHibernate的:选择@@ IDENTITY
Events.Tests.Events.Tasks.EventTasksTests.CanCreateEvent : NHibernate.PropertyValueException:ErrorDefault属性值的Events.Domain.RadioButtonQuestion._Events.Domain.Event.RadioButtonQuestionsIndexBackref错误 ----> System.IndexOutOfRangeException:此SqlCeParameterCollection不包含ParameterIndex为'3'的SqlCeParameter。
因此,如果一列真的被引用两次,那么导致该行为的FNH配置有什么问题?我正在尝试一个双向关系(一个事件有很多单选按钮问题)与订购(我会保持它,因为NH不会在一个比尔关系,从我读过的)。 FWIW我也试图通过从RadioButtonQuestion
中删除Event
并将它作为单向关系,并且它仍导致相同的异常。
FWIW,我可以得到异常去如果我使用Inverse()而不是Not.Inverse(),但这看起来很奇怪......父母应该管理这个,所以我想要Not.Inverse(),我想。 –
这个问题的第一句话是我需要的答案,措辞很好,谢谢! – MrBoJangles