0

I'm trying to implement table-per-hierarchy approach, using FluentNHibernate AutoMapping.
I have a base class and three child classes. The third child class overrides a couple of properties of the base class. Instances of first two child classes save fine, but when I try to save an instance of the third class with overridden properties I get

Invalid index 7 for this SqlParameterCollection with Count=7.

It seems NHibernate doesn't add SqlParamaters for overridden properties, but it tries to set a value.

How to fix that? Maybe some changes in mapping/configuration?

The StackTrace:

[IndexOutOfRangeException: Invalid index 7 for this SqlParameterCollection with Count=7.]
   System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32 index) +5033831
   System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32 index) +21
   System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index) +10
   NHibernate.Type.DateTimeType.Set(IDbCommand st, Object value, Int32 index) +74
   NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) +70
   NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) +37
   NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) +189
   NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) +498
   NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) +166
   NHibernate.Action.EntityInsertAction.Execute() +181
   NHibernate.Engine.ActionQueue.Execute(IExecutable executable) +42
   NHibernate.Engine.ActionQueue.ExecuteActions(IList list) +59
   NHibernate.Engine.ActionQueue.ExecuteActions() +16
   NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) +121
   NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) +92
   NHibernate.Impl.SessionImpl.Flush() +291
   MDT.Core.DataAccess.DbContext.Flush() in C:\dev\DHL\ISS\Common\MDT.Core\DataAccess\DbContext.cs:215
   ISS.Web.Controllers.ReportScheduleController.Index() in C:\dev\DHL\ISS\ISS.Web\Controllers\ReportScheduleController.cs:55
   lambda_method(Closure , ControllerBase , Object[] ) +62
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862381
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Igor
  • 131
  • 1
  • 9
  • I assume you've tried googling that error message? There are quite a lot of google hits, including many SO questions and answers. https://www.google.co.uk/search?q=Invalid%20index%20for%20this%20SqlParameterCollection%20with%20Count Two common solutions seem to be non-nullable types and duplicate mappings. – Jamie Kitson Nov 26 '12 at 17:07
  • Thank you, Jamie! Yes, I have tried googling using the subject of this question - nothing useful. Your query gives better results, thanks :) – Igor Nov 26 '12 at 17:19

1 Answers1

0

Yes, FluentNHibernate AutoMapping adds duplicate mapping for overridden properties.
It is strange that the code doesn't help

.Override<ThirdClass>(map => { map.IgnoreProperty(p => p.Age); }) 

So I have to use

.OverrideAll(map => map.IgnoreProperties(x => x.MemberInfo.HasAttribute<IgnoreMapAttribute>()))

and set [IgnoreMap] attribute in my class.

Igor
  • 131
  • 1
  • 9