4

我读过所有的帖子,并知道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并将它作为单向关系,并且它仍导致相同的异常。

+0

FWIW,我可以得到异常去如果我使用Inverse()而不是Not.Inverse(),但这看起来很奇怪......父母应该管理这个,所以我想要Not.Inverse(),我想。 –

+0

这个问题的第一句话是我需要的答案,措辞很好,谢谢! – MrBoJangles

回答

3

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

此外,RadioButtonQuestions表中的EventId列不可为空,如果集合映射不相反,则会导致问题。请参阅文档中的note

+0

烦人。我真的想要双向关系......如果在这种情况下这是正确的模型,并且重新阅读双向文档,它似乎是唯一的选择,我想我可以忍受Inverse()。我想知道为什么是这样。猜测NH真的不希望你使用bidir ...你也失去了自动列表索引。至于可为空,我的单元测试创​​建和更新正在通过,但我会留意它。 –

6

我正在使用的代码映射(NH 3.3.1),我已经注意到,添加更新(假)和插入(假)治愈的问题:

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

太棒了。这解决了我的问题。我使用的是FluentNHibernate,需要使用:'.Not.Update()。Not.Insert()',但它仍然解决了它。谢谢! –