流利的NHibernate - IndexOutOfRange [英] Fluent NHibernate - IndexOutOfRange
问题描述
Events
表中的一个Insert,然后是一个 IndexOutOfRangeException
code> RadioButtonQuestions 表。我看不到它正在尝试使用的SQL生成异常。我尝试过使用AutoMapping,现在已经切换到完整的 ClassMap
这两个类来试图缩小这个问题。 <$公共类RadioButtonQuestion:实体
{
[必需的]
public virtual Event Event {get;内部保护组}
[必须]
公共虚拟字符串GroupIntroText {get;组; }
public class事件:实体
{
[必需的]
公共虚拟字符串标题{get;组; }
[必须]
公共虚拟DateTime EventDate {get;组; }
公共虚拟IList< RadioButtonQuestions> RadioButtonQuestions {get;内部保护组}
}
公共类RadioButtonQuestionMap:ClassMap< RadioButtonQuestion>
{
public RadioButtonQuestionMap()
{
表(RadioButtonQuestions);
Id(x => x.Id).Column(RadioButtonQuestionId)。GeneratedBy.Identity();
Map(x => x.GroupIntroText);
引用(x => x.Event).Not.Nullable();
$ b 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();
$ / code $ / pre
$ b $ p生成的SQL看起来是正确的: b
$ b
create table事件(
EventId INT IDENTITY不为NULL,
EventDate DATETIME不为空,
Title NVARCHAR(255 )不为空,
主键(EventId)
)
创建表RadioButtonQuestions(
RadioButtonQuestionId INT IDENTITY NOT NULL,
GroupIntroText NVARCHAR(255)not null,
EventId INT not null,
ListIndex INT null,
主键(RadioButtonQuestionId)
)
这是使用NH 3.3.0.4000和FNH 1.3.0.727。当我尝试保存一个新的事件(与一个RadioButtonQuestion附加),我看到/ />
NHibernate:插入事件(EventDate,标题)VALUES(@ p0,@ p1); @ p0 = 5/21/2012 12:32:11 PM [Type:DateTime(0)],@ p1 ='My Test Event'[Type:String(0)]
NHibernate:select @@ IDENTITY
Events.Tests.Events.Tasks.EventTasksTests.CanCreateEvent:
NHibernate.PropertyValueException:Error属性值为Events.Domain.RadioButtonQuestion._Events.Domain.Event .RadioButtonQuestionsIndexBackref
----> System.IndexOutOfRangeException:这个SqlCeParameterCollection不包含ParameterIndex'3'的SqlCeParameter。
RadioButtonQuestion
中删除 Event
来尝试这种单向关系,它仍然导致相同的异常。 / b>您有一个双向的关联,所以一方应该被标记为逆(),并且只能是RadioButtonQuestions集合。如果您希望集合成为所有者,则必须移除对RadioButtonQuestion类中的事件的引用。 另外,RadioButtonQuestions表中的EventId列不是如果集合映射不是相反的,则可能会导致问题。请参阅文档中的注意事项。
I've read all the posts and know that IndexOutOfRange usually happens because a column is being referenced twice. But I don't see how that's happening based on my mappings. With SHOW_SQL true in the config, I see an Insert into the Events
table and then an IndexOutOfRangeException
that refers to the RadioButtonQuestions
table. I can't see the SQL it's trying to use that generates the exception. I tried using AutoMapping and have now switched to full ClassMap
for these two classes to try to narrow down the problem.
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();
}
}
The generated SQL looks correct:
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)
)
This is using NH 3.3.0.4000 and FNH 1.3.0.727. When I try to save a new Event (with a RadioButtonQuestion attached) I see
NHibernate: INSERT INTO Events (EventDate, Title) VALUES (@p0, @p1);@p0 = 5/21/2012 12:32:11 PM [Type: DateTime (0)], @p1 = 'My Test Event' [Type: String (0)] NHibernate: select @@IDENTITY
Events.Tests.Events.Tasks.EventTasksTests.CanCreateEvent: NHibernate.PropertyValueException : Error dehydrating property value for Events.Domain.RadioButtonQuestion._Events.Domain.Event.RadioButtonQuestionsIndexBackref ----> System.IndexOutOfRangeException : An SqlCeParameter with ParameterIndex '3' is not contained by this SqlCeParameterCollection.
So if a column really is being referenced twice, what's the problem with my FNH config that's causing that behavior? I'm trying for a bidirection relationship (One Event Has Many Radio Button Questions) with ordering (I'll maintain it since NH won't in a bidir relationship, from what I've read). FWIW I also tried this as a unidirectional relationship by removing the Event
from RadioButtonQuestion
and it still caused the same exception.
You have a bidirectional association, so one side should be marked as Inverse() and that can only be the RadioButtonQuestions collection. If you want the collection to be the owner, you have to remove the reference to the event in your RadioButtonQuestion class.
Additionally, the EventId column in the table RadioButtonQuestions is not nullable, which can cause problems, if the collection mapping is not inverse. See the note in the documentation.
这篇关于流利的NHibernate - IndexOutOfRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!