1

This exact title can be found in google or here at stackflow.com many times but the reason I am getting this error is like none of them: I have no problem in the unit test code but the exact same code in the application causes this issue. I wish it was because of a mapping issue.

Here is my mapping:

     <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="My.Domain" namespace="My.Domain">
      <class name="My.Domain.ReportFormFlag, My.Domain" table="ReportFormFlag" schema="Core">
        <id name="FlagID">
          <column name="FlagID" sql-type="uniqueidentifier" not-null="true"/>
          <generator class="guid"/>
        </id>
        <property name="ReportInstanceID" column="ReportInstanceID" type="int" not-null="true"/>
        <property name="FormID" column="FormID" type="int" not-null="true"/>
        <property name="SiteOrProjectID" column="SiteOrProjectID" type="int" not-null="true"/>
        <property name="IsFlagged" column="IsFlagged" type="int" not-null="true"/>
        <property name="FlagComment" column="FlagComment" type="string" not-null="false"/>
      </class>
    </hibernate-mapping>

and here is my class

namespace My.Domain
{
    [Serializable]
    public class ReportFormFlag
    {
        public virtual Guid FlagID { get; set; }    
        public virtual int ReportInstanceID { get; set; }
        public virtual int FormID { get; set; }
        public virtual int SiteOrProjectID { get; set; }
        public virtual int IsFlagged { get; set; }
        public virtual string FlagComment { get; set; }
    }
}

The code I use to insert a new record:

 var reportFormFlag = new ReportFormFlag
            {
                ReportInstanceID = 3554,
                FormID = 25,
                SiteOrProjectID = 0,
                FlagComment = "test",
                IsFlagged = 1
            };
_provider.Save(reportFormFlag);

In NUnit test it works as expected but in the application the save() caused the "Invalid index N for this SqlParameterCollection with Count=N" exception.

I am using NH 3.0, C#3.5, SQL Server 2008 R2.

Any insight is greatly appreciated!

John
  • 691
  • 1
  • 8
  • 23
  • You say that it runs fine in unit test... Is that against the same database? With the same _provider? – Goblin Jul 22 '12 at 19:57

2 Answers2

3

Typically that means you have a field mapped twice in the entity.

Craig
  • 36,306
  • 34
  • 114
  • 197
  • Thanks Graig, for your quick response. I wish that was the case but as you can see from the mapping I posted there is no field mapped twice. What troubles me the most is that the same code runs fine in the unit test. – John Jul 19 '12 at 03:55
  • or you are adding the same mapping file twice. – Rippo Jul 19 '12 at 13:44
  • Nope. I double checked everything. – John Jul 19 '12 at 22:52
  • 2
    Update. It was mapping problem but not because a database field is mapped twice. I experienced this issue before but forgot all about it. The issue is related to how nullable data fields of certain types such as integer and datetime are mapped. If a field of int or datetiem is nullable in db, the corresponding property must be set to nullable as well. In C#, that is realized by adding "?" to the type of the property. Hope this helps someone. – John Aug 08 '12 at 21:19
  • 2
    @John, you should post that as a separate answer and accept it. This allows people to quickly identify that an answer has been found for your question. – ps2goat Aug 01 '14 at 07:17
2

Thanks for the reminder, @ps2goat. Just saw your comments today!

It was mapping problem but not because a database field is mapped twice. I experienced this issue before but forgot all about it. The issue is related to how nullable data fields of certain types such as integer and datetime are mapped. If a field of int or datetiem is nullable in db, the corresponding property must be set to nullable as well. In C#, that is realized by adding "?" to the type of the property. Hope this helps someone.

John
  • 691
  • 1
  • 8
  • 23