本文介绍了ArgumentOutOfRangeException:索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 每当我使用 PersitenceSpecification类来验证对一个值对象具有引用的实体时,我得到这个奇怪的ArgumentOutOfRangeException。 public class CatalogItem:DomainEntity { internal virtual Manufacturer Manufacturer {get;私人套; } 内部虚拟字符串名称{get;私人设置} $ b保护的CatalogItem() {} $ b $公共CatalogItem(字符串名称,字符串制造商) { Name = name; 制造商=新制造商(制造商); } } 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)); $ b [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(); $ b [TestFixture] public class NHibernateSpecification :Specification { protected ISession Session {get;私人设置} 保护覆盖无效Establish_context() { var配置=新的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 void ProvideInitialData(ISession session) code $ 这里是我得到的错误: TestCase 'Then_a_catalog_object_should_be_persistable'not执行: System.ArgumentOutOfRangeException:索引超出范围。必须是非负数,小于的大小。参数名称:索引在System.ThrowHelper.ThrowArgumentOutOfRangeException (ExceptionArgument参数, ExceptionResource资源)在System.ThrowHelper.ThrowArgumentOutOfRangeException()$ b $在System.Collections。 Generic.List 1.get_Item(Int32 index) System.Data.SQLite.SQLiteParameterCollection.GetParameter(Int32 index) System.Data .Common.DbParameterCollection.System.Collections.IList.get_Item (Int32索引)在NHibernate.Type.GuidType.Set(IDbCommand cmd,对象值,Int32索引) NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd,Object value,Int32 index)在$ NHibernate.Type.NullableType.NullSafeSet(IDbCommand st,对象值,Int32索引, ISessionImplementor session) NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate (Object id,Object [] fields,Obj ect rowId,Boolean [] includeProperty,布尔型[] [] includeColumns,Int32 表,IDbCommand语句, ISessionImplementor会话,Int32 索引)在NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object $ b $ id,Object [] fields,Boolean [] notNull,Int32 j,SqlCommandInfo sql, Object obj,ISessionImplementor Session) NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object $ b $ id,Object [] fields,Object obj, ISessionImplementor session) NHibernate.Action.EntityInsertAction在NHibernate.Engine.ActionQueue.Execute(IExecutable 可执行文件)上执行() $ NHibernate.Engine.ActionQueue.ExecuteActions(IList 列表) NHibernate .Engine.ActionQueue.ExecuteActions()在NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions (IEventSource会话)在NH在NHibernate.Impl.SessionImpl.Flush()在NHibernate.Transaction.AdoTransaction.Commit()(FlushEvent事件): \Builds\FluentNH\src\FluentNHibernate\Testing \PersistenceSpecification.cs(127,0): at FluentNHibernate.Testing.PersistenceSpecification 1.TransactionalSave (Object propertyValue)d:\Builds\FluentNH\src\FluentNHibernate\Testing \PersistenceSpecification.cs(105,0): 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_b e_persistable () 很抱歉,这个帖子让我忙了几个现在几小时。这可能不是由FNH造成的,因为我发现NH本身的这个JIRA票提到了类似的东西: http://forum.hibernate.org/viewtopic.php?p=2395409 我仍然希望我在代码中做错了什么:-)。任何的想法? 提前致谢 解决方案我找到了解决这个问题的方法,这个问题是由我自己的愚蠢造成的。一旦我从流利的NH映射中生成hbm文件,这一切就变得很清楚了。 < class name =CatalogItemtable =`CatalogItemxmlns =urn:nhibernate- mapping-2.2optimistic-lock =version> ... < property name =Namelength =100type =String> < column name =Name/> < / property> ... < component name =Manufacturerinsert =falseupdate =true> < property name =Namelength =100type =String> < column name =Name/> < / property> < / component> < / class> 请注意,Name属性的列和 Manufacturer组件的列都是映射到同一列。这就是为什么这导致了一个ArgumentOutOfRangeException,因为有更多的参数比列名称。我通过明确指定了组件映射的列名来解决这个问题:元组(catalogItem => catalogItem.Manufacturer,m => m。地图(制造商=> manufacturer.Name,制造商)); 另一个教训。 I'm getting this weird ArgumentOutOfRangeException whenever I use thePersitenceSpecification class for verifying an entity that has areference to a value object. public class CatalogItem : DomainEntity { internal virtual Manufacturer Manufacturer { get; privateset; } 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, newRetailerPersistenceModel()); 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 ()Sorry for the long post, but this one got me busy for a couple ofhours now. This might not be caused by FNH as I found this JIRA ticketof NH itself that mentions something similar:http://forum.hibernate.org/viewtopic.php?p=2395409I'm still hoping that I'm doing something wrong in my code :-). Anythought?Thanks in advance 解决方案 I found the solution to this problem which resulted from my ownstupidity in the first place. It all became clear to me as soon as Igenerated the hbm files from the fluent NH mapping.<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 theManufacturer component are both mapped to the same column. That's whythis resulted into an ArgumentOutOfRangeException, because there weremore arguments than there were column names. I solved this byexplicitely specifying a column name for the component mapping:Component(catalogItem => catalogItem.Manufacturer, m => m.Map(manufacturer => manufacturer.Name,"Manufacturer"));Another lesson learned. 这篇关于ArgumentOutOfRangeException:索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-23 17:06