2

我有一个非常简单的数据库表,它以 many2many 关系连接另外两个表。

(几乎让用户提出问题——想想书签)

此表的我的 NHibernate 实体如下所示:

public class UserToRequestSubscription
{
    public virtual int Id{get;set;}
    public vitual int UserId{get;set;}
    public virtual int RequestId { get; set; }
    public virtual bool AcceptedByRequester { get; set; }
}

我想做的是添加一个新属性:

    public virtual RequestForHelp Request { get; set; }

到目前为止,我可以让它为选择工作。在我的UserToRequestSubscriptionMapping我可以简单地做:References(x => x.Request)

我遇到的问题是尝试插入一个新UserToRequestSubscription的抛出:Invalid index 6 for this SqlParameterCollection with Count=6.

我已经阅读了这个错误,当我删除RequestId属性时它消失了,但是没有这个,插入新订阅时不会填充数据库中的 RequestId 字段。

我该怎么做呢?

我的目的是节省对数据库的额外调用RequestForHelp

这是映射类:

public UserToRequestSubscriptionMapping()
{
      Id(x => x.Id);
      Map(x => x.UserId);
      Map(x => x.RequestId);
      Map(x => x.CreatedDate);
      Map(x => x.AcceptedByRequester);
      Map(x => x.IsActive);
      Map(x => x.DeactivatedDate);
      References(x => x.Request).Column("RequestId");
      Table("TheTable");
  }
4

1 回答 1

2

我猜问题在于您根据映射两次引用 RequestId 。我认为RequestId一旦添加,该属性是多余的Request

public class UserToRequestSubscription
{
    public virtual int Id{get;set;}
    public vitual User User{get;set;} // you'd probably also change this, too
    public virtual RequestForHelp Request { get; set; } // changed
    public virtual bool AcceptedByRequester { get; set; }
}

public UserToRequestSubscriptionMapping()
{
    Id(x => x.Id);
    Map(x => x.CreatedDate);
    Map(x => x.AcceptedByRequester);
    Map(x => x.IsActive);
    Map(x => x.DeactivatedDate);
    References(x => x.Request).Column("RequestId");
    References(x => x.User).Column("UserId");
    Table("TheTable");
}
于 2012-06-19T09:26:48.527 回答