0

我試圖使用FluentNHibernate AutoMapping實現每層表的方法。
我有一個基類和三個子類。第三個子類覆蓋了基類的幾個屬性。前兩個子類的實例保存很好,但是當我試圖保存與重寫特性第三類的實例,我得到NHibernate缺少SqlParameter for overriden屬性

Invalid index 7 for this SqlParameterCollection with Count=7. 

看來NHibernate的不被替代的屬性添加SqlParamaters,但它試圖設置一個值。

如何解決這個問題?也許映射/配置有些變化?

堆棧跟蹤:

[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 
+0

我假設你已經嘗試使用Google搜索該錯誤消息?有相當多的谷歌命中,包括許多SO問題和答案。 https://www.google.co.uk/search?q=Invalid%20index%20for%20this%20SqlParameterCollection%20with%20Count兩種常見的解決方案似乎是不可空的類型和重複映射。 –

+0

謝謝,傑米!是的,我嘗試使用這個問題的主題Google搜索 - 沒有用處。您的查詢提供更好的結果,謝謝:) – Igor

回答

0

是,FluentNHibernate自動映射增加了複製映射重寫屬性。
奇怪的是,代碼並不能幫助

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

所以我必須使用

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

,並在我的課設置[IgnoreMap]屬性。