title: key-many-to-one and key-property association: nhibernate won't DELETE items from set
body: <p>I'll try to keep this terse, but hopefully won't miss any important information in my troubles.  The code I believe provides all details, but I've left out the noise (it's VB, so there's lots of noise :) ).</p>
  
  <p>A "Case" object has many "Assignments":</p>
  
  <pre><code>Public Class Case
    Property CaseId As Guid 
    Property Assignments As ISet(Of CaseAssignment)
  End Class
  
  Public Class CaseAssignment
    Property Case As Case
    Property RoleId As Guid
  End Class
  </code></pre>
  
  <p>The data model I've been handed looks about like what you'd expect, except CaseAssignment is a composite key:</p>
  
  <pre><code>table Case
     CaseId uniqueid not null primary key
     ...
  
  table CaseAssignment
     CaseId uniqueid not null
     RoleId uniqueid not null
     PK := (CaseId, RoleId)
     FK from CaseId -&gt; Case.CaseId
  </code></pre>
  
  <p>Finally, the Fluent NHibernate Mappings:</p>
  
  <pre><code>Class CaseMapping
    Public Sub New()
      Table("Case")
      KeyColumn("CaseId")
      HasMany(Function(x) x.Assignments).KeyColumn("CaseId").Cascade.AllDeleteOrphan()
  End Class
  
  Class CaseAssignmentMapping
    Public Sub New()
      Table("CaseAssignment")
      CompositeId() _
        .KeyReference(Function(x) x.Case, "CaseId") _
        .KeyProperty(Function(x) x.RoleId)
  End Class
  </code></pre>
  
  <p>KeyReference correlates with "key-many-to-one" in the XML mapping lingo.</p>
  
  <p>When I add assignments to a case all is good, but when I remove references I get one of two problems.  With this code:</p>
  
  <pre><code>aCase.Assignments.Remove(someAssignment)
  caseRepository.Save(aCase)
  </code></pre>
  
  <p>The error I get back is, "Could not delete collection rows... Cannot insert the value NULL into column 'CaseId', table 'CaseAssignments'; column does not allow nulls. UPDATE fails.
  The statement has been terminated."  This was from trying to issue the following SQL:</p>
  
  <pre><code>UPDATE CaseAssignments SET CaseId = null
  WHERE CaseId = @p0 AND RoleId = @p1 AND CaseId = @p2
  @p0=[valid guid #1],
  @p1=[valid guid #2],
  @p2=[valid guid #1 again] **!?!**
  </code></pre>
  
  <p>So that's a little messed up.  So I try this code:</p>
  
  <pre><code>aCase.Assignments.Remove(someAssignment)
  someAssignment.Case = Nothing
  caseRepository.Save(aCase)
  </code></pre>
  
  <p>and the error is "Unexpected row count: 0; expected: 1" because NHibernate tried to:
  DELETE FROM CaseAssignments WHERE RoleId = [valid guid] AND CaseId = <strong>NULL</strong></p>
  
  <p>I've been scouring threads and forums and the NHibernate and Hibernate docs and haven't really come across anything similar yet.  Hopefully it's something simple.  Thanks to anyone who takes a shot at this one!</p>