nhibernate - 流畅的 NHibernate - IndexOutOfRange

标签 nhibernate fluent-nhibernate fluent-nhibernate-mapping nhibernate-3

我已经阅读了所有帖子并且知道 IndexOutOfRange 通常发生是因为一个列被引用了两次。但是根据我的映射,我不知道这是如何发生的。在配置中 SHOW_SQL 为 true 时,我看到一个 Insert into the Events表,然后是 IndexOutOfRangeException指的是 RadioButtonQuestions table 。我看不到它试图使用的生成异常的 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();
    }
}

生成的 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 Events (EventDate, Title) VALUES (@p0, @p1);@p0 = 5/21/2012 12:32:11 PM [Type: DateTime (0)], @p1 = 'My Test Event' [类型:字符串 (0)]
NHibernate:选择@@IDENTITY

Events.Tests.Events.Tasks.EventTasksTests.CanCreateEvent:
NHibernate.PropertyValueException:使 Events.Domain.RadioButtonQuestion._Events.Domain.Event.RadioButtonQuestionsIndexBackref 的属性值脱水时出错
----> System.IndexOutOfRangeException:此 SqlCeParameterCollection 不包含参数索引为“3”的 SqlCeParameter。

因此,如果某个列确实被引用了两次,那么导致该行为的 FNH 配置有什么问题?我正在尝试使用排序的双向关系(一个事件有很多单选按钮问题)(我将维护它,因为根据我的阅读,NH 不会处于双向关系中)。 FWIW 我也通过删除 Event 尝试将其作为单向关系。来自 RadioButtonQuestion它仍然导致相同的异常。

最佳答案

我在代码(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);
});

关于nhibernate - 流畅的 NHibernate - IndexOutOfRange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10689054/

相关文章:

nhibernate - 为所有域类添加带有接口(interface)的 IAutoMappingOverride

.net - 免费的 NHibernate 辅助工具?

c# - NHibernate 在 WHERE IN 中使用 QueryOver

c# - NHibernate Query to json 结果出乎意料

asp.net-mvc - 如何验证自定义属性?

c# - Nhibernate:使用已定义的映射调用带有 CreateSqlQuery 的存储过程

.net - 流利的 NHibernate : What is AutoImport?

azure - 无法在 Azure 移动服务上找到程序集

c# - Fluent NHibernate AutoMapping,应该可以节省时间,但这让我很抓狂

nhibernate - Fluent NHibernate 尝试使用 table-per-subclass 映射子类的子类