异常:指数超出范围

时间:2022-10-29 16:40:22

I'm getting this weird ArgumentOutOfRangeException whenever I use the PersitenceSpecification class for verifying an entity that has a reference to a value object.

每当我使用持久化特殊类来验证一个对值对象有引用的实体时,都会得到这个奇怪的ArgumentOutOfRangeException。

    public class CatalogItem : DomainEntity
    {
        internal virtual Manufacturer Manufacturer { get; private
set; }
        internal virtual String Name { get; private set; }

        protected CatalogItem()
        {}

        public CatalogItem(String name, String manufacturer)
        {
            Name = name;
            Manufacturer = new Manufacturer(manufacturer);
        }
    }

    public class CatalogItemMapping : ClassMap<CatalogItem>
    {
        public CatalogItemMapping()
        {
            Id(catalogItem => catalogItem.Id);

            Component<Manufacturer>(category => category.Manufacturer,
                                    m => m.Map(manufacturer =>
manufacturer.Name));

            Map(catalogItem => catalogItem.Name);
            Map(Reveal.Property<CatalogItem>("Price"));
        }
    }

    [TestFixture]
    public class When_verifying_the_class_mapping_of_a_catalog_item
        : NHibernateSpecification
    {
        [Test]
        public void Then_a_catalog_object_should_be_persistable()
        {
            new PersistenceSpecification<CatalogItem>(Session)
                .VerifyTheMappings();
        }
    }

    [TestFixture]
    public class NHibernateSpecification
        : Specification
    {
        protected ISession Session { get; private set; }

        protected override void Establish_context()
        {
            var configuration = new SQLiteConfiguration()
                .InMemory()
                .ShowSql()
                .ToProperties();

            var sessionSource = new SessionSource(configuration, new
RetailerPersistenceModel());
            Session = sessionSource.CreateSession();

            sessionSource.BuildSchema(Session);
            ProvideInitialData(Session);

            Session.Flush();
            Session.Clear();
        }

        protected override void Dispose_context()
        {
            Session.Dispose();
            Session = null;
        }

        protected virtual void ProvideInitialData(ISession session)
        {}
    }

Here's the error I'm getting:

我得到的错误是:

TestCase 'Then_a_catalog_object_should_be_persistable' not executed: System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.ThrowHelper.ThrowArgumentOutOfRangeException (ExceptionArgument argument, ExceptionResource resource) at System.ThrowHelper.ThrowArgumentOutOfRangeException() at System.Collections.Generic.List1.get_Item(Int32 index) at System.Data.SQLite.SQLiteParameterCollection.GetParameter(Int32 index) at System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item (Int32 index) at NHibernate.Type.GuidType.Set(IDbCommand cmd, Object value, Int32 index) at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate (Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) at NHibernate.Engine.ActionQueue.ExecuteActions() at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions (IEventSource session) at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush (FlushEvent event) at NHibernate.Impl.SessionImpl.Flush() at NHibernate.Transaction.AdoTransaction.Commit() d:\Builds\FluentNH\src\FluentNHibernate\Testing \PersistenceSpecification.cs(127,0): at FluentNHibernate.Testing.PersistenceSpecification1.TransactionalSave (Object propertyValue) d:\Builds\FluentNH\src\FluentNHibernate\Testing \PersistenceSpecification.cs(105,0): at FluentNHibernate.Testing.PersistenceSpecification`1.VerifyTheMappings () C:\Source\SupplyChain\Tests\Retailer.IntegrationTests\Mappings \CatalogItemMappingSpecifications.cs(14,0): at SupplyChain.Retailer.IntegrationTests.Mappings.When_verifying_the_class_mapping_of_a_catalog_item.Then_a_catalog_object_should_be_persistable ()

TestCase“Then_a_catalog_object_should_be_persistable”未执行:System。异常:指数超出范围。必须是非负的并且小于集合的大小。参数名称:System.ThrowHelper的索引。如:,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,在System.Data.SQLite.SQLiteParameterCollection get_Item(Int32指数)。在System.Data.Common.DbParameterCollection.System.Collections.IList GetParameter(Int32指数)。get_Item (Int32索引)位于NHibernate.Type.GuidType。设置(IDbCommand cmd,对象值,Int32索引)在NHibernate.Type.NullableType。nhibernate . type . NullSafeSet(IDbCommand cmd, Object value, Int32 index)。NullSafeSet(IDbCommand st . Object value, Int32 index, ISessionImplementor session)。脱水(对象id、对象[]字段、对象rowId、布尔[]includeProperty、布尔[]includeColumns、Int32 table、IDbCommand语句、ISessionImplementor session、Int32 index)。插入(对象id,对象[]字段,Boolean[] notNull, Int32 j, SqlCommandInfo sql,对象obj, ISessionImplementor session)在nnate.persister . persistence . entity.abstractentitypersister。插入(对象id、对象[]字段、对象obj、ISessionImplementor会话)在NHibernate.Action.EntityInsertAction.Execute()在NHibernate.Engine.ActionQueue。在NHibernate.Engine.ActionQueue执行(IExecutable可执行)。在nhibernate . engineering . action .ExecuteActions()在NHibernate.Event.Default.AbstractFlushingEventListener中。在NHibernate.Event.Default.DefaultFlushEventListener中执行(IEventSource会话)。在NHibernate.Impl.SessionImpl.Flush()在nhibernate . transaction . adotransactionl . commit () d:只\FluentNH\src\ src\ fluentnrc \FluentNHibernate\ FluentNHibernate\ FluentNHibernate\测试\ \ \ \ \ \ \ \ \ persistencspecific . htm .cs .cs(127、0):TransactionalSave (Object propertyValue) d:\建立\FluentNH\src\ src\FluentNHibernate测试\ persistencparticular .cs(105,0):在FluentNHibernate. test . persistencfication ' 1。VerifyTheMappings()C:\ \ SupplyChain \测试\零售商来源。IntegrationTests \ \ CatalogItemMappingSpecifications.cs映射(0):SupplyChain.Retailer.IntegrationTests.Mappings.When_verifying_the_class_mapping_of_a_catalog_item。Then_a_catalog_object_should_be_persistable()

Sorry for the long post, but this one got me busy for a couple of hours now. This might not be caused by FNH as I found this JIRA ticket of NH itself that mentions something similar:

很抱歉这么长时间,但这一次让我忙了几个小时。这可能不是FNH造成的,因为我发现NH本身的JIRA票据中提到了类似的东西:

http://forum.hibernate.org/viewtopic.php?p=2395409

http://forum.hibernate.org/viewtopic.php?p=2395409

I'm still hoping that I'm doing something wrong in my code :-). Any thought?

我仍然希望我的代码做错了什么:-)。任何想法吗?

Thanks in advance

谢谢提前

6 个解决方案

#1


15  

I found the solution to this problem which resulted from my own stupidity in the first place. It all became clear to me as soon as I generated the hbm files from the fluent NH mapping.

我找到了解决这个问题的办法,这首先是由于我自己的愚蠢。当我从fluent NH映射生成hbm文件时,一切都变得清晰起来。

<class name="CatalogItem" table="`CatalogItem`" xmlns="urn:nhibernate-
mapping-2.2" optimistic-lock="version">
    ...

    <property name="Name" length="100" type="String">
      <column name="Name" />
    </property>

    ...

    <component name="Manufacturer" insert="false" update="true">
      <property name="Name" length="100" type="String">
        <column name="Name" />
      </property>
    </component>
  </class>

Notice that the column for the Name property and the column for the Manufacturer component are both mapped to the same column. That's why this resulted into an ArgumentOutOfRangeException, because there were more arguments than there were column names. I solved this by explicitely specifying a column name for the component mapping:

注意,Name属性的列和Manufacturer组件的列都映射到同一列。这就是为什么这会导致ArgumentOutOfRangeException,因为参数比列名多。通过为组件映射指定列名,我明确地解决了这个问题:

Component(catalogItem => catalogItem.Manufacturer, m => m.Map(manufacturer => manufacturer.Name, "Manufacturer"));

组件(catalogItem = > catalogItem。制造商,m = > m。地图(制造商= >制造商。的名字,“制造商”));

Another lesson learned.

另一个教训。

#2


2  

In my case, I was mapping two properties to the same column with Fluent NHibernate.

在我的例子中,我将两个属性映射到同一列,并具有流畅的NHibernate。

#3


0  

Your CatalogItem doesn't seem to have a Price property, which seems odd when you're using the Reveal helper.

您的编目项似乎没有价格属性,这在使用“显示助手”时显得很奇怪。

#4


0  

Yes, I removed that one for reducing some of the noise. I guess I forgot to remove it from the mapping as well. After doing some more investigation, I noticed that it has something to do with Manufacturer being mapped as a component. When I used a plain-old string instead of a separate class, everything works fine.

是的,为了减少一些噪音,我去掉了那个。我想我也忘记把它从映射中删除了。在做了更多的调查之后,我注意到它与制造商被映射为一个组件有关。当我使用一个普通的字符串而不是一个单独的类时,一切都很正常。

#5


0  

In my particular case I was adding property as well as ID (using attributes) over the same .NET property. This resulted in the same error.

在我的例子中,我在同一个。net属性上添加了属性和ID(使用属性)。这导致了同样的错误。

#6


0  

Old question, but if someone runs into the same problem as I did, it may help to know that this Fluent Nhibernate issue (ColumnPrefix only applied to first Component mapping inside a ComponentMap) can give the same exception since the columns prefixes are not always applied.

老问题,但是如果有人遇到了和我一样的问题,那么了解这个Fluent Nhibernate问题(仅应用于ComponentMap中的第一个组件映射的column前缀)可能会有帮助,因为列前缀并不总是被应用。

#1


15  

I found the solution to this problem which resulted from my own stupidity in the first place. It all became clear to me as soon as I generated the hbm files from the fluent NH mapping.

我找到了解决这个问题的办法,这首先是由于我自己的愚蠢。当我从fluent NH映射生成hbm文件时,一切都变得清晰起来。

<class name="CatalogItem" table="`CatalogItem`" xmlns="urn:nhibernate-
mapping-2.2" optimistic-lock="version">
    ...

    <property name="Name" length="100" type="String">
      <column name="Name" />
    </property>

    ...

    <component name="Manufacturer" insert="false" update="true">
      <property name="Name" length="100" type="String">
        <column name="Name" />
      </property>
    </component>
  </class>

Notice that the column for the Name property and the column for the Manufacturer component are both mapped to the same column. That's why this resulted into an ArgumentOutOfRangeException, because there were more arguments than there were column names. I solved this by explicitely specifying a column name for the component mapping:

注意,Name属性的列和Manufacturer组件的列都映射到同一列。这就是为什么这会导致ArgumentOutOfRangeException,因为参数比列名多。通过为组件映射指定列名,我明确地解决了这个问题:

Component(catalogItem => catalogItem.Manufacturer, m => m.Map(manufacturer => manufacturer.Name, "Manufacturer"));

组件(catalogItem = > catalogItem。制造商,m = > m。地图(制造商= >制造商。的名字,“制造商”));

Another lesson learned.

另一个教训。

#2


2  

In my case, I was mapping two properties to the same column with Fluent NHibernate.

在我的例子中,我将两个属性映射到同一列,并具有流畅的NHibernate。

#3


0  

Your CatalogItem doesn't seem to have a Price property, which seems odd when you're using the Reveal helper.

您的编目项似乎没有价格属性,这在使用“显示助手”时显得很奇怪。

#4


0  

Yes, I removed that one for reducing some of the noise. I guess I forgot to remove it from the mapping as well. After doing some more investigation, I noticed that it has something to do with Manufacturer being mapped as a component. When I used a plain-old string instead of a separate class, everything works fine.

是的,为了减少一些噪音,我去掉了那个。我想我也忘记把它从映射中删除了。在做了更多的调查之后,我注意到它与制造商被映射为一个组件有关。当我使用一个普通的字符串而不是一个单独的类时,一切都很正常。

#5


0  

In my particular case I was adding property as well as ID (using attributes) over the same .NET property. This resulted in the same error.

在我的例子中,我在同一个。net属性上添加了属性和ID(使用属性)。这导致了同样的错误。

#6


0  

Old question, but if someone runs into the same problem as I did, it may help to know that this Fluent Nhibernate issue (ColumnPrefix only applied to first Component mapping inside a ComponentMap) can give the same exception since the columns prefixes are not always applied.

老问题,但是如果有人遇到了和我一样的问题,那么了解这个Fluent Nhibernate问题(仅应用于ComponentMap中的第一个组件映射的column前缀)可能会有帮助,因为列前缀并不总是被应用。