[英]NHibernate ERROR Message Invalid index 4 for this SqlParameterCollection with Count=4
NHibernate ERROR Message Invalid index 4 for this SqlParameterCollection with Count=4.
NHibernate错误消息Count = 4的此SqlParameterCollection的索引无效4。
My Mapping: 我的地图:
public KazangAccountClassMap()
{
Table("KazangAccount");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Channel).Not.Nullable();
Map(x => x.UserName).Not.Nullable();
Map(x => x.Password).Not.Nullable();
HasMany<KazangMerchant>(x => x.KazangMerchants)
.KeyColumn("AccountId")
.Inverse();
}
CONTROLLER: 控制器:
public ActionResult SaveChannelProperty(string NewProp, string ChannelName)
{
client.CreateKazangChannelAttribute(new KazangChannelAttr
{
KazangChannelId = ChannelObject.Id,
AttrName = NewProp,
AttrValue = NewProp,
AttrType = ""
});
}
This kind of exception is almost always related to "doubled" mapping. 这种异常几乎总是与“两倍”映射有关。 Because from the snippets shown in the quesiton it is not easy to show, I would explain it like this:
因为从问题中显示的摘录中很难显示出来,所以我将这样解释:
Let's have an entity KazangAccount
, which has reference to Channel
and also int
representation of that object as ChannelId
让我们有一个实体
KazangAccount
,它引用Channel
并将该对象的int
表示形式为ChannelId
public class KazangAccount
{
public virtual int Id { get; set; }
...
public virtual Channel Channel { get; set; }
public virtual int ChannelId { get; set; }
}
In this case, we would be intstructing NHibernate to use ONE column for TWO properties - and that would not work: 在这种情况下,我们将指示NHibernate对两个属性使用ONE列 -这将不起作用:
public KazangAccountClassMap()
{
...
References(x => x.Channel)
.Column("Channel_ID"); // the Channel reference
Map(x => x.ChannelId)
.Column("Channel_ID"); // the int property ChannleId
That mapping is good for any READ operation... column will be used twice. 该映射对于任何READ操作都非常有用...列将被使用两次。 But for WRITE operation, we would create params for column
Channel_ID
and another for column Channel_ID
== only one of them will be created. 但对于写操作,我们将创建一个列PARAMS
Channel_ID
和另一列Channel_ID
==只有其中一人将被创建。 And while NHibernate expects some count of params - there is one missing. 尽管NHibernate期望有一些参数-但是缺少一种。
Solution? 解? Make one of these read-only
将其中之一设为只读
public KazangAccountClassMap()
{
...
References(x => x.Channel)
.Column("Channel_ID"); // READ and WRITE
Map(x => x.ChannelId)
.Column("Channel_ID") // just READ
.ReadOnly() // insert="false" update="false"
;
Now, NHibernate will create/expect only one column/sqlParamter for this column... 现在,NHibernate将只为该列创建/预期一个列/ sqlParamter ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.