此SqlParameterCollection的索引无效13,其Count = 13 [英] Invalid index 13 for this SqlParameterCollection with Count=13
问题描述
这个确切的标题可以在Google或这里的stackflow.com上找到很多次,但是出现此错误的原因却与它们都不一样:我在单元测试代码中没有问题,但是在应用程序中代码完全相同导致此问题.我希望是由于映射问题.
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.
这是我的地图:
<?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);
在NUnit测试中,它可以按预期工作,但是在应用程序中,save()导致了此Count = N的SqlParameterCollection的索引N无效".
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.
我正在使用NH 3.0,C#3.5,SQL Server 2008 R2.
I am using NH 3.0, C#3.5, SQL Server 2008 R2.
任何见解都将不胜感激!
Any insight is greatly appreciated!
推荐答案
感谢您@@ ps2goat的提醒.今天刚刚看到您的评论!
Thanks for the reminder, @ps2goat. Just saw your comments today!
这是映射问题,但不是因为数据库字段被映射两次.我之前曾遇到过这个问题,但是却一无所获.问题与某些类型(如整数和日期时间)的可为空的数据字段如何映射有关.如果int或datetiem的字段在db中可为空,则相应的属性也必须设置为可为空.在C#中,可以通过添加?"来实现属性的类型.希望这对某人有帮助.
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.
这篇关于此SqlParameterCollection的索引无效13,其Count = 13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!