我为我的NHibernate数据库模型定义了一个复合id。我也使用了外键关系中定义的一个列。如果我使用NHibernate插入一个对象,我会得到以下异常:
System.IndexOutOfRangeException:此SqlParameterCollection的索引7无效,Count = 7。
在System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32索引)
在System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32索引)
在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)
在NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st,Object value,Int32 begin,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.Insert(Object id,Object [] fields,Boolean [] notNull,Int32 j,SqlCommandInfo sql,Object obj,ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id,Object [] fields,Object obj,ISessionImplementor session)
在NHibernate.Impl.StatelessSessionImpl.Insert(String entityName,Object entity)
在NHibernate.Impl.StatelessSessionImpl.Insert(对象实体)
是否可以将一列用于两个不同的目的(外键关系和主键)?
C#NHibernate模型
public const string STR_ID = "Id";
public const string STR_SHARDKEY = "ShardKey";
public const string STR_OTHERMODEL = "OtherModel";
[CompositeId(-4)]
[KeyProperty(-3, Name = STR_ID, Column = STR_ID)]
[KeyProperty(-2, Name = STR_SHARDKEY , Column = STR_SHARDKEY )]
[Generator(-1, Class = "guid.comb")]
public virtual Guid? Id { get; set; }
[Column(Name = STR_SHARDKEY )]
public virtual int ShardKey{ get; set; }
[ManyToOne(0)]
[Column(1, Name = STR_OTHERMODEL )]
[Column(2, Name = STR_SHARDKEY )]
public virtual OtherModel OtherModel { get; set; }
XML映射
<composite-id>
<key-property name="Id" column="Id" />
<key-property name="ShardKey" column="ShardKey" />
</composite-id>
<many-to-one name="OtherModel" >
<column name="OtherModel" />
<column name="ShardKey" />
</many-to-one>