Jamie Dixon
Jamie Dixon

Reputation: 54001

FluentNHibernate mapping a join table with extra data

I have a very simple database table that joins two other tables in a many2many relationship.

(pretty much joining users to questions - think bookmarking)

My NHibernate entity for this table looks like this:

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; }
}

What I'd like to do is add a new property:

    public virtual RequestForHelp Request { get; set; }

So far I can get this to work for selects. In my UserToRequestSubscriptionMapping I can simply do: References(x => x.Request).

The problem I'm having is when trying to insert a new UserToRequestSubscription which throws: Invalid index 6 for this SqlParameterCollection with Count=6..

I've read up on this error and it vanishes when I remove the RequestId property however without this, the RequestId field in the DB is not populated when inserting a new subscription.

How do I go about doing this?

My aim here is to save doing extra calls to the DB for the RequestForHelp items.

Here's the mapping class:

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");
  }

Upvotes: 2

Views: 935

Answers (1)

moribvndvs
moribvndvs

Reputation: 42497

I would guess the problem lies in that you reference RequestId twice according to your mapping. I think the RequestId property is superfluous once you add 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");
}

Upvotes: 2

Related Questions