IndexOutOfRangeException在NHibernate的深处

时间:2010-02-19 16:47:45

标签: nhibernate exception fluent-nhibernate nhibernate-mapping mapping

我有以下映射:

public class SecurityMap : ClassMap<Security>
    {
        public SecurityMap()
        {
            Table("Security");
            CompositeId().KeyProperty(k => k.Id, "SecurityId").KeyProperty(k => k.EndDate);
            Map(x => x.LastUpdateUser);
            References(x => x.Company).Columns("CompanyId", "EndDate");
            References(x => x.PrimaryListing).Columns("PrimaryListingId", "EndDate");
         }
    }

public class ListingMap : ClassMap<Listing>
    {
        public ListingMap()
        {
            Table("Listing");
            CompositeId().KeyProperty(k => k.Id, "ListingID").KeyProperty(k => k.EndDate);
            References(x => x.Security).Columns("SecurityId","EndDate");
        }
    }

 public class CompanyMap : ClassMap<Company>
    {
        public CompanyMap()
        {
            Table("Company");
            CompositeId().KeyProperty(k => k.Id, "CompanyID").KeyProperty(k => k.EndDate);
            HasMany(x => x.Securities).KeyColumns.Add("CompanyId", "EndDate");
        }       
    }

当我尝试运行此测试时:

[Test]
public void can_update_a_security()
{
    var repo = IoC.Resolve<ISecurityRepository>();
    int someSecurity = 1;
    using (var work = IoC.Resolve<IUnitOfWorkManager>().Current)
    {
        Security security = repo.Get(someSecurity);
        security.ShouldNotBeNull();
        security.LastUpdateUser = "Dirk Diggler" + DateTime.Now.Ticks;
        repo.Save(security);
        work.Commit();
    }
}

我在NHibernate的内容深处得到以下错误:

  

执行       System.IndexOutOfRangeException:此索引6无效   带有Count = 6的SqlParameterCollection。       在System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32   指数)       在System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32   指数)       在System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32)   指数)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\型号\ DateTimeType.cs(65,0):   在   NHibernate.Type.DateTimeType.Set(IDbCommand的   st,对象值,Int32索引)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\型号\ NullableType.cs(180,0):   在   NHibernate.Type.NullableType.NullSafeSet(IDbCommand的   cmd,对象值,Int32索引)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\型号\ NullableType.cs(139,0):   在   NHibernate.Type.NullableType.NullSafeSet(IDbCommand的   st,对象值,Int32索引,   ISessionImplementor会话)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\型号\ ComponentType.cs(213,0):   在   NHibernate.Type.ComponentType.NullSafeSet(IDbCommand的   st,对象值,Int32开始,   ISessionImplementor会话)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\持留\实体\ AbstractEntityPersister.cs(2393,0):   在   NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(对象   id,Object [] fields,Object rowId,   Boolean [] includeProperty,Boolean [] []   includeColumns,Int32表,   IDbCommand声明,   ISessionImplementor会话,Int32   指数)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\持留\实体\ AbstractEntityPersister.cs(2754,0):   在   NHibernate.Persister.Entity.AbstractEntityPersister.Update(对象   id,Object [] fields,Object []   oldFields,Object rowId,Boolean []   includeProperty,Int32 j,Object   oldVersion,Object obj,SqlCommandInfo   sql,ISessionImplementor session)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\持留\实体\ AbstractEntityPersister.cs(2666,0):   在   NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(对象   id,Object [] fields,Object []   oldFields,Object rowId,Boolean []   includeProperty,Int32 j,Object   oldVersion,Object obj,SqlCommandInfo   sql,ISessionImplementor session)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\持留\实体\ AbstractEntityPersister.cs(2940,0):   在   NHibernate.Persister.Entity.AbstractEntityPersister.Update(对象   id,Object [] fields,Int32 []   dirtyFields,布尔值   hasDirtyCollection,Object []   oldFields,Object oldVersion,Object   obj,Object rowId,ISessionImplementor   会话)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\行动\ EntityUpdateAction.cs(78,0):   在   NHibernate.Action.EntityUpdateAction.Execute()       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\引擎\ ActionQueue.cs(130,0):   在   NHibernate.Engine.ActionQueue.Execute(IExecutable   可执行文件)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\引擎\ ActionQueue.cs(113,0):   在   NHibernate.Engine.ActionQueue.ExecuteActions(IList的   名单)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\引擎\ ActionQueue.cs(147,0):   在   NHibernate.Engine.ActionQueue.ExecuteActions()       S:\ NHibernate的\ NHibernate的的\ src \ NHibernate的\事件\ DEFAULT \ AbstractFlushingEventListener.cs(241,0):   在   NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource   会话)       S:\ NHibernate的\ NHibernate的的\ src \ NHibernate的\事件\ DEFAULT \ DefaultFlushEventListener.cs(19,0):   在   NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent   事件)       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\默认地将Impl \ SessionImpl.cs(1478,0):   在NHibernate.Impl.SessionImpl.Flush()       S:\ NHibernate的\ NHibernate的\ SRC \ NHibernate的\事务\ AdoTransaction.cs(187,0):   在   NHibernate.Transaction.AdoTransaction.Commit()       在lambda_method(ExecutionScope,ITransaction)

现在有趣的是,如果我在SecurityMap中注释掉对Company或PrimaryListing的引用,我就不会收到错误。我评论出来并不重要。只有当我同时出现错误时才会发生错误。

当实际更新时,NHProf会向我显示此更新:

UPDATE Security
SET    LastUpdateUser = '2010-02-19T08:09:24.00' /* @p0 */,
       CompanyId = 54199 /* @p1 */,
       EndDate = '9999-12-31T00:00:00.00' /* @p2 */
WHERE  SecurityId = 1 /* @p3 */
       AND EndDate = '9999-12-31T00:00:00.00' /* @p4 */

我不确定为什么要更新CompanyId和EndDate,但我怀疑它是相关的。

有人有想法吗?非常感谢周围的工作。

1 个答案:

答案 0 :(得分:30)

是的,这是一个常见问题,您在映射定义中使用Column "EndDate"两次(对于公司和主要列表)并且不允许这样做。其中一个必须去,或者有一个额外的EndDate列(每个关联一个)

检查一下 nHibernate 2.0 - mapping a composite-id *and* many-to-one relationship causes "invalid index" error

http://devlicio.us/blogs/derik_whittaker/archive/2009/03/19/nhibernate-and-invalid-index-n-for-this-sqlparametercollection-with-count-n-error.aspx