“此 SqlParameterCollection 的索引 n 无效,Count=n";OR “外键不能为空" [英] "Invalid Index n for this SqlParameterCollection with Count=n" OR "foreign key cannot be null"
问题描述
我已经成功使用 NHibernate 有一段时间了,并且已经能够通过我的应用程序解决很多陷阱用它开发并在生产中运行.最近的障碍不过,真的让我摸不着头脑.
I have been successfully using NHibernate for quite some time now and have been able to solve a lot of pitfalls with an application that I developed with it and that is running in production. The recent hurdle really has me scratching my head, though.
最近我不得不用一些新的类来扩展类库嵌套为一些现有类的子类.我刚复制与我已经成功的聚合映射相同的模型使用,但这次它不起作用.
Recently I had to expand the class library with some new classes that are nested as children to some already existing classes. I just copied the same model for aggregate mapping that I already was successfully using, but this time it does not work.
现在,当我在父映射文件中使用以下内容时:
Now when I use the following in the parent mapping file:
<bag name="SeaInfoItems" table="EDIImport_SeaInfo" lazy="false" cascade="save-update">
<key column="EDI_FK_OWNERID"/>
<one-to-many class="FargoGate.AppLib.EdiImportSeaInfo, FargoGate.AppLib"/>
</bag>
我可以选择在子类中使用:
I can choose to, in the child class, either use:
<property name="EDI_FK_OWNERID" column="EDI_FK_OWNERID" />
...这给了我臭名昭著的Invalid Index n for thisSqlParameterCollection with Count=n"错误.
...which gives me the infamous "Invalid Index n for this SqlParameterCollection with Count=n" error.
或者我尝试使用我在谷歌搜索后找到的这个解决方案:
OR I try with this solution I found after some Googling:
<property name="EDI_FK_OWNERID" column="EDI_FK_OWNERID" insert="false" update="false" />
...这给了我一个无法将值 NULL 插入列'EDI_FK_OWNERID'...列不允许为空."错误.
...which gives me a "Cannot insert the value NULL into column 'EDI_FK_OWNERID'... column does not allow nulls." error.
所以基本上我必须在害虫和霍乱之间做出选择.
So basically I have to choose between pest and cholera.
我不明白的是它对于已经存在的聚合类,我真的看不出有什么区别.唯一的事情是这个外键(EDI_FK_OWNERID)可以引用两个不同的父表.糟糕的数据库设计,我知道,但我没有设计它,我的任务是发展它是好是坏.我无法更改数据库设计.
What I don't get is that it works flawlessly for the already existing aggregate classes, and I really cannot spot the difference. The only thing is that this foreign key (EDI_FK_OWNERID) could refer to two different parent tables. Bad database design, I know, but I didn't design it, and it is my task to develop up to it for better or worse. I cannot change the database design.
另一个区别是我从已经存在的子类(映射以及类成员)中完全删除了外键引用.我当然试图效仿,但无济于事.
The other difference is that I totally removed the foreign key reference from the already existing child classes (the mappings as well as the class members). I tried to emulate that of course, but of no avail.
我还发现其中一个新类(很小)也可以正常工作.但我也看不出这里有什么区别.我难住了!
Also I discovered that one of the new classes (which is quite small) also works fine. But I cannot see what the difference is here either. I am stumped!
有人知道吗?
推荐答案
啊!我被这个臭名昭著的Invalid Index n for this SqlParameterCollection with Count=n"错误严重误导,我忽略了一个明显的问题:一个类的字段的重复映射.在那个特定的映射中,我留下了这个错误,其中主键也被定义为一个属性:
Aaargh! I was put so much on a wrong leg with this infamous "Invalid Index n for this SqlParameterCollection with Count=n" error that I overlooked the obvious: A duplicate mapping of a field for ONE of the classes. In that particular mapping I left this error, where the primary key is also defined as a property:
<id name="ID" column="ID">
<generator class="guid" />
</id>
<property name="ID" column="ID" />
现在尝试调试是浪费时间!
Now that was a waste of time trying to debug that!
这篇关于“此 SqlParameterCollection 的索引 n 无效,Count=n";OR “外键不能为空"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!