使用嵌套集合查看对象不会引发错误,但是在尝试保存实体时出现错误

    [IndexOutOfRangeException: Invalid index 15 for SqlParameterCollection with Count=15.]
   System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32 index) +5343807
   System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32 index) +19
   System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index) +10
   NHibernate.Type.Int64Type.Set(IDbCommand rs, Object value, Int32 index) +60
   NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) +414
   NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) +62
   NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) +122
   NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) +344

[PropertyValueException: Error dehydrating property value for PravUprav.Classes.Entities.ClaimCaseInstanceFI.Decision]
   NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) +503
   NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) +2013
   NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) +335
   NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session) +1898
   NHibernate.Action.EntityUpdateAction.Execute() +737
   NHibernate.Engine.ActionQueue.Execute(IExecutable executable) +39
   NHibernate.Engine.ActionQueue.ExecuteActions(IList list) +128
   NHibernate.Engine.ActionQueue.ExecuteActions() +48
   NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) +241
   NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) +179
   NHibernate.Impl.SessionImpl.Flush() +295
   NHibernate.Transaction.AdoTransaction.Commit() +189
   Spring.Data.NHibernate.HibernateTransactionManager.DoCommit(DefaultTransactionStatus status) in c:\_prj\spring-net\src\Spring\Spring.Data.NHibernate\Data\NHibernate\HibernateTransactionManager.cs:561

[DataIntegrityViolationException: Error dehydrating property value for PravUprav.Classes.Entities.ClaimCaseInstanceFI.Decision]
   Spring.Data.NHibernate.HibernateTransactionManager.DoCommit(DefaultTransactionStatus status) in c:\_prj\spring-net\src\Spring\Spring.Data.NHibernate\Data\NHibernate\HibernateTransactionManager.cs:577


错误:

Error dehydrating property value for


说我两次映射属性或实体属性的DataTypes错误,但是我检查并在代码中未找到错误。

具有集合的实体:

public class ClaimCase
{
    private IList<ClaimCaseInstance> _claimCaseInstances;
    public virtual IList<ClaimCaseInstance> ClaimCaseInstances
    {
       get { return _claimCaseInstances ?? (_claimCaseInstances = new List<ClaimCaseInstance>()); }
          set { _claimCaseInstances = value; }
     }
}


实体映射:

public sealed class ClaimCaseMap : ClassMap<ClaimCase>
{
   public ClaimCaseMap()
      {
         Table("ClaimCase");
         Id(x => x.Id);
         HasMany(x => x.ClaimCaseInstances).Inverse().Cascade.AllDeleteOrphan();
       }
 }


ClaimCaseInstance实体:

public abstract class ClaimCaseInstance
{
    public virtual ClaimCase ClaimCase { get; set; }
    public virtual DicList Result { get; set; }
}


子班:

public class ClaimCaseInstanceFI : ClaimCaseInstance
{
    public virtual DicList Decision { get; set; }
}

public class ClaimCaseInstanceAI : ClaimCaseInstance
{
}


对应:

public sealed class ClaimCaseInstanceMap : ClassMap<ClaimCaseInstance>
{
    public ClaimCaseInstanceMap()
    {
        Table("ClaimCaseInstances");
        Id(x => x.Id);
        DiscriminateSubClassesOnColumn("InstanceType");

        References(x => x.ClaimCase);
        References(x => x.Result);
    }
}

public sealed class ClaimCaseInstanceFIMap : SubclassMap<ClaimCaseInstanceFI>
{
    public ClaimCaseInstanceFIMap()
    {
        DiscriminatorValue((int)CourtTypes.Firts);
        References(x => x.Decision);
    }
}

public sealed class ClaimCaseInstanceAIMap : SubclassMap<ClaimCaseInstanceAI>
{
    public ClaimCaseInstanceAIMap()
    {
        DiscriminatorValue((int)CourtTypes.Appeals);
    }
}


法院类型:

public enum CourtTypes
{
    First = 5,
    Appeals = 6
}


DicList按照约定ForeignKeyConversion作为instance.Property.Name + "ID"引用映射,并且工作正常。

仅在编辑字段ClaimCase后保存Decision时发生错误

最佳答案

我已经确定了问题的根源。
使用出色的工具dotPeek(感谢@SergSW作为提示)作为symbol serverdebug

NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate()


我发现sql语句中的更新字段数不相等

entityMetamodel.PropertySpan


数组entityMetamodel.Properties中的某些字段出现了两次。

public sealed class ClaimCaseInstanceMap : ClassMap<ClaimCaseInstance>
    {
        public ClaimCaseInstanceMap()
        {
            Table("ClaimCaseInstances");

            DiscriminateSubClassesOnColumn("InstanceType");

            Id(x => x.Id);
            References(x => x.ClaimCase);
            Map(x => x.FilingDate);
            References(x => x.Result);
            Map(x => x.PriceResult);
            Map(x => x.ExpenseResult);
            Map(x => x.Comment);
        }
    }


子类:

public sealed class ClaimCaseInstanceFIMap : SubclassMap<ClaimCaseInstanceFI>
{
    public ClaimCaseInstanceFIMap()
    {
        DiscriminatorValue((int)CourtType.Firts);

        Id(x => x.Id);
        Map(x => x.Price);
        Map(x => x.Expense);
        References(x => x.Decision);
        References(x => x.Determination);
        Map(x => x.DeterminationDate);
        Map(x => x.CourtDateAct);
        Map(x => x.PriceResult);
        Map(x => x.ExpenseResult);
        Map(x => x.CounterClaimDate);
        Map(x => x.Comment);
    }
}


如您所见,该属性为:CommentExpenseResultPriceResult

我没有描述问题中所有字段的映射,这是我的错误,对此非常抱歉,谢谢您的帮助。

关于c# - FluentNHibernate:使用区分符映射子集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28317913/

10-16 06:56