仅当关联表具有空记录时,此 SqlParameterCollection 的索引 N 无效且 Count=N
我有一个相当复杂的实体,当特定数据库表丢失记录时,该实体将不会保存。当记录存在时,实体正确保存。当记录没有时,我收到异常:
此 SqlParameterCollection 的索引 N 无效且 Count=N
阅读了通过 Google 找到的一堆解决方案以及 Stack Overflow 上最密切相关的问题后:
- 什么是导致“此 SqlParameterCollection 的索引 nn 无效 with Count=nn”,当数据库中的列为 Null 时?
- 此 SqlParameterCollection with Count=n 的索引 n 无效”或“外键 不能为空
我相信我的问题与我设置映射文件的方式有关。 Customer 实体引用了 Person 实体。 Person 映射到我们已读取但没有写入权限的表。当 Person 实体的记录不存在时,我会生成异常。如果记录存在就没有问题。我已将客户对 Person 的引用设置为 Nullable()
。我还进行了双重检查,以确保没有从任一实体映射两次的属性。
以下是我认为相关的映射信息,但可以根据需要提供更多信息:
Customer
//more mapping code...
References(x => x.Person, "snl_id").Nullable();
//more mapping code...
Person
//more mapping code...
ReadOnly();
Id(x => x.SnlId).Column("SNL_ID");
//more mapping code...
为了使事情进一步复杂化,我们有一些痛苦的代码来使 NHibernate 在 Person 时表现得更好不存在。我不确定它是否适用于此,但认为它足以包含在我的问题中。我们使用下面的代码,因为如果没有它,NHibernate JIRA 将创建大量查询。 Stack Overflow 答案中概述了此解决方案。
客户的个人属性
public virtual Person Person
{
get
{
try
{
var snlId = per.Name;
return per;
}
catch
{
return null;
}
}
set
{
per = value;
}
}
private EPerson per;
我的映射中缺少什么会导致此异常?这个问题还有我没有看到的吗?
I have a rather complex entity which will not save when a particular database table is missing a record. When the record exists the entity saves correctly. When the record does not I receive the exception:
Invalid index N for this SqlParameterCollection with Count=N
After reading a bunch of solutions found via Google and the most closely related questions on Stack Overflow:
- What's causing “Invalid index nn for this SqlParameterCollection
with Count=nn” when a column is Null in the database? - Invalid Index n for this SqlParameterCollection with Count=n” OR “foreign key
cannot be null
I believe my issue has to do with the way I have my mapping files setup. The Customer entity has reference to the Person entity. Person maps to a table which we have read, but not write access to. It is when a record for the Person entity does not exist that I generate the exception. If the record exists no issue. I've set the reference to Person from customer to Nullable()
. I have also double checked to ensure I do not have a property mapped twice from either entity.
Here is what I feel is the pertinent mapping information, but can provide more as needed:
Customer
//more mapping code...
References(x => x.Person, "snl_id").Nullable();
//more mapping code...
Person
//more mapping code...
ReadOnly();
Id(x => x.SnlId).Column("SNL_ID");
//more mapping code...
To further complicate matters we have some painful code to make NHibernate perform better when Person does not exist. I am not sure it applies here, but thought it pertinent enough to include in my question. We are using the below code because without it the NHibernate JIRA will create tons of queries. This solution is outlined in this Stack Overflow answer.
Customer's person property
public virtual Person Person
{
get
{
try
{
var snlId = per.Name;
return per;
}
catch
{
return null;
}
}
set
{
per = value;
}
}
private EPerson per;
What am I missing in my mappings that would cause this exception? Is there another piece of this problem that I am not seeing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然 Scott 从 Customer 类中删除 snl_id 属性的解决方案修复了该问题,但它会导致我无法解决的问题 - 即使没有相应的 Person 表记录,snl_id 也可以存在于 Customer 表中。由于这种情况,有时当我无法通过关联的 Person 属性访问 snl_id 时,我需要访问它。
我考虑了几种替代解决方案,但最终决定创建 Customer 表的视图,包括 Customer 表主键和客户表中的 snl_id。然后通过连接将该属性映射到视图。
这个改变让我鱼与熊掌兼得。我能够保留客户的 SnlId 属性,但在保存时不再抛出异常。
While Scott's solution of removing the snl_id property from the Customer class fixes the issue it causes problems that I cannot get around-- the snl_id can exist in the Customer table even there is not a corresponding Person table record. Since that is the case there are times when I will need access to the snl_id when I cannot get to it via the associated Person property.
I considered several alternative solutions but settled on creating a view of the Customer table including the Customer table primary key and the snl_id from the customer table. Then mapping that property via a join to the view.
This change allowed me to have my cake and eat it to. I was able to keep the SnlId property on customer, but no longer throw the exception when saving.
您是否将 snl_id 作为 Customer 中的属性引用并作为子对象的主键?如果是这样,这将导致您收到错误。从 Customer 中删除该属性并使用 Person 来获取该值。
Do you have the snl_id referenced as a property in Customer as well as being the primary key for the child object? If so, this is causing the error you are receiving. Remove the property from Customer and use Person to get the value.