Infamous: Invalid index n for this SqlParameterCollection with Count=

别来无恙 提交于 2019-12-03 23:17:34
Rippo

The answer is either:-

a) you have a duplicate property mapped in the same class

b) It is possible if you are exposing a foreign-key as well as using a <many-to-one ... to the related entity in the mapping file. If this is the case add insert="false" and update="false" to the foreign key property and run again.

To verify this, as you are using fluent and automapping, you need to look at the XML mappings. See this [link][2] and use ExportTo(..) method. Once you have done this look at the XML and see if you have any duplicate properties OR even duplicate mapping files.

In you case, you have two references to column GI:

<id name="Id" ...>
  <column name="GI" />
  <generator class="assigned" />
</id>

<property name="GI" ...>
  <column name="GI" />
</property>

I take it you can't set the annotation [DocumentId] on the Id class property. I think you may need to abandon auto mapping for this class and configure via fluent manually!

With full credit to @Rippo, the equivalent answer in Fluent NHibernate which helped me is:

For the classes:

public class User
{
   public virtual Department {get; set;}
}

public class Department
{
   public virtual ICollection<User> Users {get; set;}
}

If you have the following mapping for the User entity:

//Problem mapping
Map(x => x.DepartmentId)          
References(x => x.Department)
   .Column("Id")
   .ForeignKey("DepartmentId")
   .Fetch.Join();

The following is one of the possible solutions (due to double mapping in the one-to-manypart - one-Department-to-many-Users):

// !Solution
Map(x => x.DepartmentId)          
References(x => x.Department)
   .Column("Id")
   .ForeignKey("DepartmentId")
   .Fetch.Join()
   .Not.Insert()   // <- added this
   .Not.Update();  // <- and this

I had this error where in my Fluent IAutoMappingOverride class I had a mapping.IgnoreProperty(p => Property) where Property was only a getter. I removed the IgnoreMap statement and it fixed it. This is with NH 3.3.1.4. Probably doesn't relate to your issue, but hopefully this will help someone else.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!