21

This exception:

Invalid index n for this SqlParameterCollection with Count=

Usually points at duplicate mapping information (see Stack Overflow + Google). I am pretty sure I have none. Are there any other reasons for it?

I seem to have identified the problem. I introduced this:

[DocumentId]
public virtual int GI
{
    get { return base.Id; }
    protected set { base.Id = value; }
} 

To use search via lucene.net. This seems to interfere with FNH! What are my options here?

PS:

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.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)
   at NHibernate.Action.EntityInsertAction.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.DefaultFlushEventListener.OnFlush(FlushEvent event)
   at NHibernate.Impl.SessionImpl.Flush()
   at SharpArch.Data.NHibernate.DbContext.CommitChanges()
   at Updater1.Program.Main(String[] args) in C:\Users\bla\Documents\Visual Studio 2010\Projects\Bla\Updater1\Program.cs:line 97

PPS:

public class MappedSequenceMap : IAutoMappingOverride<MappedSequence>
    {
        public void Override(AutoMapping<MappedSequence> mapping)
        {
            mapping.Id(x => x.Id, "GI").GeneratedBy.Assigned();

            mapping.Map(x => x.Affiliation).Length(10000);
            mapping.Map(x => x.Gene).Length(10000);
            mapping.Map(x => x.OriginalIsolationCountry).Length(10000);
            mapping.Map(x => x.OriginalAffiliation).Length(10000);
            mapping.Map(x => x.PMIDs).Length(10000);
            mapping.Map(x => x.Product).Length(10000);
            mapping.Map(x => x.Fasta).Length(10000);
            mapping.Map(x => x.Note).Length(10000);
            mapping.Map(x => x.Strain).Length(10000);

            mapping.HasManyToMany(x => x.PubmedPublications).Table("SequencesPubmedPublications");
        }
    }
cs0815
  • 16,751
  • 45
  • 136
  • 299
  • 1
    I said that I googled and searched SO (see question)!!! There is no HQL involved as it is a simple Save() of an entity via a s#arp repository. – cs0815 May 23 '12 at 10:43
  • I would re-post the full exception (exception.ToString()) and post the relevant mapping just to be sure. – Cole W May 23 '12 at 11:46
  • Need to see the full mapping for the entity in question – Rippo May 23 '12 at 12:35

3 Answers3

58

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!

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Rippo
  • 22,117
  • 14
  • 78
  • 117
  • @bartoszKP due to your editing this does not read very well at all now :) – Rippo May 09 '17 at 18:58
  • Sorry, for making your post confusing. My intent was to make it a complete answer without any historical noise. It was helpful for me, but it was a bit hard to navigate through all the PPSes ;) How is it now? Of course feel free to improve the post even more, especially if I'm still missing what you wanted to say in the answer :) – BartoszKP May 09 '17 at 20:21
6

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
tinonetic
  • 7,751
  • 11
  • 54
  • 79
1

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.

David Fidge
  • 451
  • 5
  • 6