4

在使用 NHibernate 时,我花了大部分时间尝试解决此消息:“索引超出范围。必须为非负数且小于集合的大小。参数名称:索引”

我的更新和删除工作正常,但在调用 save() 后调用刷新不起作用。我认为发布所有代码(有很多)没有用,因为我不确定您此时需要看到什么。如果有帮助,我尝试保存的数据访问对象使用复合键。另外,我正在使用 NHibernate.Mapping.Attributes 创建我的映射。

在我的堆栈跟踪中,NHibernate 似乎试图将无效数量的参数发送到 ADO.Net 类以进行插入操作。话虽如此,我已将跟踪设置为 true 以查看 sql 语句,但在将 sql 语句发送到输出之前引发了错误,因此我不确定它试图构建什么。

public void Insert(ProddataDAO Entity)
{
        Entity.PSEQ = GetNewSeqID(Entity.PCUST); // <- because I'm using a composite key, this function gets my a unique value by a group
        NHibernateHelperCGC.CurrentSession.Save(Entity);            
        NHibernateHelperCGC.CurrentSession.Flush();   // <-This raises the error         
}

在 System.Collections.ArrayList.get_Item(Int32 index) 在 IBM.Data.DB2.iSeries.iDB2ParameterCollection.System.Collections.IList.get_Item(Int32 index) 在 NHibernate.Type.DecimalType.Set(IDbCommand st, Object value, Int32 index) 在 NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) 在 NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session) 在 NHibernate.Type.ComponentType.NullSafeSet( IDbCommand st,对象值,Int32 开始,ISessionImplementor 会话)在 NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(对象 id,Object[] 字段,对象 rowId,Boolean[] includeProperty,Boolean[][] includeColumns,Int32 表,IDbCommand语句,ISessionImplementor 会话,Int32 索引)在 NHibernate.Persister。NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields,对象 obj,ISessionImplementor 会话)在 NHibernate.Engine.ActionQueue.Execute(IExecutable 可执行文件)在 NHibernate.Engine.ActionQueue.ExecuteActions(IList 列表)在 NHibernate.Engine.ActionQueue.ExecuteActions()的 NHibernate.Action.EntityInsertAction.Execute()在 NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) 在 NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent 事件) 在 NHibernate.Impl.SessionImpl.Flush() 在 WTS.Data.DB2.CGC.ProddataRepository.Insert (ProddataDAO 实体)在 C 中:\Documents and Settings\briant\My Documents\Visual Studio 2008\Projects\WTSCGCDAL\WTSCGCDAL\ProddataRepository.cs:C:\Documents and Settings\briant\My Documents\Visual 中 WTS.WTSCGCAppInterface.ProductionDataUserControlBase.TestInsert() 的第 73 行Studio 2008\Projects\WTSCGCAppInterface\WTSCGCAppInterface\ProductionDataUserControlBase.cs:在 C:\Documents and Settings\briant\My Documents\Visual Studio 2008\Projects\WTSCGCAppInterface\WTSCGCAppInterface.Window1.Button_Click_1(Object sender, RoutedEventArgs e) 的第 483 行\Window1.xaml.cs:System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 的第 48 行,System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 的 System.Windows.UIElement。RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 在 System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) 在 System.Windows.Controls.Primitives.ButtonBase.OnClick() 在 System.Windows.Controls.Button.OnClick() 在 System.Windows .Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 在 System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) 在 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 在 System.Windows.RoutedEventArgs .InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args,Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e) at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) ) 在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 在 System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 在 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 在 System System.Windows.UIElement.RaiseEventImpl(DependencyObject 发件人,System.Windows.Input.InputManager.ProcessStagingArea() 在 System.Windows.Input.InputManager.ProcessInput(InputEventArgs 输入) 在 System.Windows.Input 的 System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trust) .InputProviderSite.ReportInput(InputReport inputReport) 在 System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode 模式, Int32 时间戳, RawMouseActions 操作, Int32 x, Int32 y, Int32 wheel) 在 System.Windows.Interop.HwndMouseInputProvider.FilterMessage (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 处理) 在 System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 处理) 在 MS.Win32.HwndWrapper.WndProc (IntPtr hwnd,Int32 消息,IntPtr wParam, IntPtr lParam, Boolean&handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) at System.Windows.Threading.ExceptionWrapper。 TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl (DispatcherPriority 优先级,TimeSpan 超时,委托方法,对象参数,布尔 isSingleParameter)在 MS.Win32 的 System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority 优先级,委托方法,对象参数)。HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 在 System.Windows.Threading.Dispatcher 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) .PushFrame(DispatcherFrame 框架)
在 System.Windows.Threading.Dispatcher.Run() 在 System.Windows.Application.RunDispatcher(Object ignore) 在 System.Windows.Application.RunInternal(Window window) 在 System.Windows.Application.Run(Window window) 在 System .Windows.Application.Run() at WTSCGCAppInterface.App.Main() 在 C:\Documents and Settings\briant\My Documents\Visual Studio 2008\Projects\WTSCGCAppInterface\WTSCGCAppInterface\obj\Debug\App.g.cs:line 0 在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading .ThreadHelper.ThreadStart_Context(对象状态)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart()

4

2 回答 2

6

当表中缺少列时,通常会出现此错误。因此,如果一个类具有映射到数据库中缺少的表列的属性,那么您将看到此错误。

基本上检查映射和数据库表之间的不匹配(缺少列、类型不匹配等)。问题可能出在相关表/实体上,而不是您要保存的表/实体上。

于 2009-12-10T12:00:01.537 回答
1

我也遇到了这个问题,经过数小时的谷歌搜索,我发现这是映射中的一个问题:我有一个相同的列name两次(在我的情况下,它既是 a <key-property>,作为 a 的一部分<composite-id>,也是<many-to-one>元素。我解决了更改<key-property><key-many-to-one>删除<many-to-one>元素)。

于 2015-06-16T12:15:43.513 回答