title: EF Core - Error when adding a related entity
body: <p>I get an error when I try to update a related entity of an entity that I already got from database. For illustration purposes I have these entites: </p>
  
  <pre><code>    class Car
  {
     int Id ..;
     string Name ..;
     virtual ICollection&lt;TireCar&gt; tires ...;
  }
  
  class TireCar
  {
    int Id ..;
    int TireId ..;
    int CarId..;
    int Size..;
    virtual TireBrand tire;
    virtual Car car;
  }
  
  class TireBrand
  {
    int Id;
    string Name ..;
  }
  </code></pre>
  
  <p>So, I'm trying to make a Patch method that allows me to update the <code>Car</code> data and also adds, updates or deletes the <code>tires</code>. The problem happens when I get the <code>Car</code> entity and after that I add a <code>Tire</code>. Something like that:</p>
  
  <pre><code>    void UpdateCar()
      {
          var car = carService.Get(...);
  
          ...
  
          carService.AddTire(new TireCar{ CarId = car.Id, TireId = 1 });
  
          ...
      }
  </code></pre>
  
  <p>I'm using the Repository pattern with DI, so the context is the same. The error that is throwing is:</p>
  
  <blockquote>
    <p><strong>System.InvalidOperationException:</strong> The association between entity types 'Car' and 'TireCar' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes.'</p>
  </blockquote>
  
  <p>I tried two things that worked but I think is not the solution:</p>
  
  <ul>
  <li>When I get the Car after adding the Tires</li>
  <li>When I get the Car as no tracking</li>
  </ul>
  
  <p>Why that happend if I'm updating another tables? What Can I do to solve this?</p>