title: EF Core: The INSERT statement conflicted with the FOREIGN KEY constraint
body: <p>I'm new in Entity Framework Core and I have a problem with the next situation.</p>
  
  <p>I have three objects, like this:</p>
  
  <pre><code>TableA {
  
      public int Id {get; set;}
      public string Name {get; set;}
  
      public IList&lt;TableA_TableB&gt; TableA_TableBList {get; set;}
  }
  
  TableA_TableB {
      public int Id {get; set;}
  
      public TableA TableA {get; set;}
      public int TableAId {get; set;}
  
      public TableB TableB {get; set;}
      public int TableBId {get; set;}
  }
  
  
  TableB {
      public int Id {get; set;}
      public string Name {get; set;}
  
      public IList&lt;TableA_TableB&gt; TableA_TableBList {get; set;}
  }
  </code></pre>
  
  <p>I'm using the Fluent Api to map the relationship:</p>
  
  <pre><code>builder
      .HasOne(a_b =&gt; a_b.TableA) 
      .WithMany(a =&gt; a.TableA_TableBList) 
      .HasForeignKey(a_b =&gt; a_b.TableAId); 
  
  builder
      .HasOne(a_b =&gt; a_b.TableB) 
      .WithMany(b =&gt; b.TableA_TableBList) 
      .HasForeignKey(a_b =&gt; a_b.TableBId); 
  </code></pre>
  
  <p>My problem is when I try to insert a new value in TableA with a new value in TableA_TablesB, but there is an existent value TableB. The code that I used is:</p>
  
  <pre><code>var entity = (TableA)tableADto; //From controller and convert to TableA using explict operator
  
  entity.TableA_TableBList = new List&lt;TableA_TableBList&gt;();
  entity.TableA_TableBList.Add(new TableA_TableB()
  {
      TableAId = entity.Id,
      TableBId = 1 // I put a fix value just to test
  });
  
  _tableARepository.Add(entity);
  _unitOfWork.Commit();
  </code></pre>
  
  <p>When I call <code>SaveChanges()</code>, this error is shown: </p>
  
  <blockquote>
    <p>The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TableA_TableB_TableB_TableBId".<br>
    The conflict occurred in database "tenantteste", table "dbo.TableB", column 'Id".</p>
  </blockquote>
  
  <p>I have tried to get TableB value and add a navigation property (TableB property). But, instead of creating a relationship, the value was added when I saved. </p>
  
  <p>I have read the documentation and some questions on stackoverflow, and I believe using FK should work, but it didn't work. <p>I don't know what I'm doing wrong.</p></p>