在Hibernate映射问题中加入表

时间:2014-07-24 12:25:37

标签: c# sql nhibernate

我的.hbm.xml文件中有第二个代码:

  <class name="Option" table="tOptions">
    <id name="Id">
      <column name="Id" sql-type="int"/>
      <generator class="native"/>
    </id>
    <property name="DocumentId" column="Ref"/>
    <property name="QuoteRef"/>
    <property name="OptionCode" column="OpCode"/>
    <property name="Description" column="Description"/>
    <property name="SupplierID" column="SupplierID"/>
    <property name="OptionTypeId" column="OptionType"/>
    <join table="tActiveOffers">
      <key column="Ref" unique="true"/>
      <property name="Colour" column="ExteriorColor"/>
      <property name="Trim" column="InteriorColor"/>
    </join>
  </class>

似乎很正常,但是当我试图从数据库中获取某些内容时,我收到了错误消息: &#34;这个sqlparametercollection的索引11无效,其中count = 11&#34;,在大多数情况下指向NHibernate问题。此外,所有工作都正常,直到2个新字段(颜色和修剪)添加到Option类。所以我认为问题在于错误的加入。

如果键列等于Id

,它将正常工作
<join table="tActiveOffers">
  <key column="Id" unique="true"/>
  <property name="Colour" column="ExteriorColor"/>
  <property name="Trim" column="InteriorColor"/>
</join>

但这对我来说并不好(Ids和两张桌子不一样)。

我猜错误的密钥或其他东西存在问题。

我缺少什么,怎么能更正确地完成?

修改 错误堆栈跟踪:

   at System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32 index)
   at System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32 index)
   at System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index)
   at NHibernate.Type.Int32Type.Set(IDbCommand rs, Object value, Int32 index)
   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
   at 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)
   at 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)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)
   at NHibernate.Action.EntityUpdateAction.Execute()
   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
   at NHibernate.Engine.ActionQueue.ExecuteActions()
   at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
   at NHibernate.Event.Default.DefaultAutoFlushEventListener.OnAutoFlush(AutoFlushEvent event)
   at NHibernate.Impl.SessionImpl.AutoFlushIfRequired(ISet`1 querySpaces)
   at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)
   at NHibernate.Impl.ExpressionQueryImpl.List()
   at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
   at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
   at Remotion.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

1 个答案:

答案 0 :(得分:0)

如果键列没有定位id,而是某个属性,我们必须使用property-ref,所以不要这样:<key column="Ref" unique="true"/>使用此

<join table="tActiveOffers">
  <key column="Ref" unique="true" property-ref="DocumentId"/>
  <property name="Colour" column="ExteriorColor"/>
  <property name="Trim" column="InteriorColor"/>
</join>