title: Deleting NHibernate mapped object not working - causes sql UPDATE
body: <p>I'm having some trouble with NHibernate, while trying to remove an object describing a relation between rows in two different tables - a many to many relation.</p>
  
  <p>I've got Users, CourseInstances and UserRoles, where Users are mapped to certain course instances. Users can be a part of many course instances, and course instances have many users attending.</p>
  
  <p>All this is mapped by Fluent.</p>
  
  <pre><code>User: HasMany(x =&gt; x.UserRoles).KeyColumn("UserId");
  CourseInstance: HasMany(x =&gt; x.UserRoles);
  UserRole:
      References(x =&gt; x.User).Column("UserId");
      References(x =&gt; x.CourseInstance).Column("InstanceId");
  </code></pre>
  
  <p>The above describes how they are mapped together, its a fairly simple mapping, though each have some additional information but nothing related to each other.</p>
  
  <p>I try to run the following code:</p>
  
  <pre><code>using (var session = factory.OpenSession())
  {
      var user = session.Get&lt;NData.User&gt;(userId);
      if (user == null)
          throw new FaultException(new FaultReason("No user with that id found."));
      var instance = session.Get&lt;NData.CourseInstance&gt;(courseInstanceId);
      if (instance == null)
          throw new FaultException(new FaultReason("No course instance with that id found."));
      var userrole = session.CreateQuery(string.Format("from UserRole where User.Id = {0} and CourseInstance.Id = {1} and Role.Role = {2}", userId, courseInstanceId, role)).UniqueResult&lt;NData.UserRole&gt;();
      if (userrole == null)
          throw new FaultException(new FaultReason("That user is not present in that course instance with that role."));
      instance.UserRoles.Remove(userrole);
      user.UserRoles.Remove(userrole);
      session.Delete(userrole);
      session.Update(user);
      session.Update(instance);
      session.Flush();
  }
  </code></pre>
  
  <p>However, Flush throws an exception:</p>
  
  <pre><code>could not delete collection rows: [Giraffe.WebService.NHibernate.Data.User.UserRoles#8][SQL: UPDATE UserRoles SET UserId = null WHERE UserId = @p0 AND Id = @p1]
  </code></pre>
  
  <p>Why is it trying to UPDATE the role first?</p>