当前位置:  开发笔记 > 编程语言 > 正文

ArgumentOutOfRangeException:索引超出范围

如何解决《ArgumentOutOfRangeException:索引超出范围》经验,为你挑选了1个好方法。

每当我使用PersitenceSpecification类来验证具有对值对象的引用的实体时,我就会得到这个奇怪的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
    {
        public CatalogItemMapping()
        {
            Id(catalogItem => catalogItem.Id);

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

            Map(catalogItem => catalogItem.Name);
            Map(Reveal.Property("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(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)
        {}
    }

这是我得到的错误:

TestCase'Then_a_catalog_object_should_be_persistable'未执行:System.ArgumentOutOfRangeException:索引超出范围.必须是非负数且小于集合的大小.参数名:System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument参数,ExceptionResource资源)的索引,位于System.Colrow.Generic.List的System.ThrowHelper.ThrowArgumentOutOfRangeException()处1.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):在SupplyChain.Retailer.IntegrationTests.Mappings.When_verifying_the_class_mapping_of_a_catalog_item.Then_a_catalog_object_should_be_persistable()

对不起,很长的帖子,但这个让我忙了几个小时了.这可能不是由FNH引起的,因为我发现NH本身的这张JIRA票据提到了类似的东西:

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

我仍然希望我的代码中出错了:-).任何想法?

提前致谢



1> user12867..:

我找到了解决这个问题的方法,这个问题首先源于我自己的愚蠢.一旦我从流畅的NH映射生成hbm文件,这一切都变得清晰.


    ...

    
      
    

    ...

    
      
        
      
    
  

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

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

另一个经验教训.

推荐阅读
Chloemw
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有