title: Deleting Many-To-Many entry
body: <p>How can one delete a Many-To-Many entry?<br>
  I have the entities <code>Client</code> and <code>Restaurant</code> and a relationship table and entity between them: <code>ClientRestaurant</code> having the following columns: <code>ClientID</code>, <code>RestaurantID</code>, <code>DateOfBill</code>.<br>
  A client entity has the navigational property <code>ClientRestaurants</code> which is a list of <code>ClientRestaurant</code>.<br>
  When I delete an entry from a <code>Client</code>'s <code>ClientRestaurants</code> list:</p>
  
  <blockquote>
    <p>myClient.ClientRestaurants.Remove(myClientRestaurants)</p>
  </blockquote>
  
  <p>and then try to <code>SaveChanges</code> I get the following error: </p>
  
  <p><strong><code>Adding a relationship with an entity which is in the deleted state is not allowed.</code></strong>  </p>
  
  <p>Update: I set the columns in ClientRestaurants table from SQL Server as Nullable. Now if I do:</p>
  
  <blockquote>
    <p>myClient.ClientRestaurants.Remove(myClientRestaurants)
    myRestaurant.ClientRestaurants.Remove(myClientRestaurants)</p>
  </blockquote>
  
  <p>and then <code>SaveChanges</code> it works fine, I mean the entries from relationships are removed, but in DB remains a row with <code>NULL</code> values, and since I have an <code>UNIQUE INDEX</code> for those two columns, I can only have one row with <code>NULL</code> values. There has to be another solution, but it eludes me.<br>
  Any help greatly appreciated.<br>
  These are the functions that add and remove records to the intermediary table.</p>
  
  <pre><code>Private Function addTehnicianToLucrare(L As Lucrare, T As Tehnician) As TehnicianLucrare
      Dim result As TehnicianLucrare = Nothing
      Dim TL As New TehnicianLucrare
      TL.Tehnician = T
      'T.LucrariTehnician.Add(TL)
      TL.Lucrare = L
      'L.TehnicieniLucrare.Add(TL)
      If TL IsNot Nothing Then result = TL
      Return result
  End Function
  Private Function removeTehnicianFromLucrare(L As Lucrare, T As Tehnician) As TehnicianLucrare
      Dim result As TehnicianLucrare = Nothing
      Dim TL As TehnicianLucrare = Me._ctx.TehnicieniLucrare.Local.Where(Function(p) p.Tehnician Is T And p.Lucrare Is L).SingleOrDefault
      If TL IsNot Nothing Then
          Me._ctx.TehnicieniLucrare.Remove(TL)
          result = TL
      End If
      Return result
  End Function
  </code></pre>
  
  <blockquote>
    <p>Consider Lucrare as Restaurant and Tehnician as Client from the above description.</p>
  </blockquote>
  
  <p>UPDATE: I have these Subs:</p>
  
  <pre><code>Private Sub list_TehnicieniRaport_DoubleClick(sender As Object, e As EventArgs) Handles list_TehnicieniRaport.DoubleClick
      Try
          If Me.list_TehnicieniRaport.SelectedIndex &gt; -1 Then
              Dim curTeh = TryCast(Me.list_TehnicieniRaport.SelectedItem, TehnicianLucrare)
              If curTeh IsNot Nothing Then
                  'setProperty("TehnicieniLucrare", curTeh.Tehnician)
                  moveToDataTable(curTeh.Tehnician)
                  'ChangeTracker.HasChanges() gives error here
              End If
          End If
      Catch ex As Exception
          logError(ex)
      End Try
  End Sub
  
  Public Sub moveToDataTable(ByRef pTehnician As Tehnician)
      Try
          setProperty("TehnicieniLucrare", pTehnician)
          If getProperty("Edit") Then selectEchipa()
          selectCar()
      Catch ex As Exception
          logError(ex)
      End Try
      'ChangeTracker.HasChanges() DOES NOT give error here.
  End Sub
  </code></pre>
  
  <p>This way I get the error, and it appears right after the <code>moveToDataTable()</code> sub exits. I had step-by-step debugging and calling <code>_ctx.ChangeTracker.HasChanges()</code> after every step.<br>
  If I move the <code>setProperty("TehnicieniLucrare", curTeh.Tehnician)</code> to the <code>list_TehnicieniRaport_DoubleClick(...)</code> sub, then I don't get the error, and everything works just fine. I don't understand.....</p>