title: Entity Framework Core Put Operation Not Working
body: <p>I am getting </p>
  
  <blockquote>
    <p>Unhandled Exception Error: the property 'Id' on entity type 'Vehicle' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principalThe property 'Id' on entity type 'Vehicle' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal</p>
  </blockquote>
  
  <p>Here is my Put API:</p>
  
  <pre><code>[HttpPut("{id}")]
  public IActionResult UpdateVehicle(int id, [FromBody] SaveVehicleResource vehicleResource)
  {
          if (!ModelState.IsValid)
              return BadRequest(ModelState);
  
          var vehicle = context.Vehicles.Include(v =&gt; v.Features).SingleOrDefault(v =&gt; v.Id == id);
  
          if (vehicle == null)
              return NotFound();
  
          mapper.Map(vehicleResource, vehicle);
          vehicle.LastUpdate = DateTime.Now;
  
          context.SaveChanges();
  
          var result = mapper.Map&lt;Vehicle, SaveVehicleResource&gt;(vehicle);
  
          return Ok(result);
  }
  </code></pre>
  
  <p>Here is <code>DbContext</code>:</p>
  
  <pre><code>public class VegaDbContext : DbContext
  {
      public DbSet&lt;Make&gt; Makes { get; set; }
      public DbSet&lt;Feature&gt; Features { get; set; }
      public DbSet&lt;Vehicle&gt; Vehicles { get; set; }
      public DbSet&lt;Model&gt; Models { get; set; }
  
      public VegaDbContext(DbContextOptions&lt;VegaDbContext&gt; options) : base(options)
      {
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;VehicleFeature&gt;().HasKey(vf =&gt; new { vf.VehicleId, vf.FeatureId });
      }
  }
  </code></pre>
  
  <p><code>Vehicle</code> class:</p>
  
  <pre><code>public class Vehicle
  {
      public int Id { get; set; }
      public int ModelId { get; set; }
      public Model Model { get; set; }
      public bool IsRegistered { get; set; }
      public Contact Contact { get; set; }
      public ICollection&lt;VehicleFeature&gt; Features { get; set; }
      public DateTime LastUpdate { get; set; }
      public Vehicle()
      {
          Features = new Collection&lt;VehicleFeature&gt;();
      }
  }
  </code></pre>
  
  <p><code>VehicleFeature</code> class:</p>
  
  <pre><code>public class VehicleFeature
  {
      public int VehicleId { get; set; }
      public int FeatureId { get; set; }
      public Vehicle Vehicle { get; set; }
      public Feature Feature { get; set; }
  }
  </code></pre>