title: How to Insert or Update in a Many to Many relationship EF?
body: <p>I am struggling with inserting or updating records in a many to many relationship.The problem is with the cross-table.I have generated the classes with Entity Framework Database First.Even if i clear the cross table and i repopulate it i get unique constraint exception.How should i proceed?</p>
  
  <p><strong>Structure:</strong></p>
  
  <p>(<strong>Story</strong>) 1------N  (<strong>StoryLocation</strong>) N-------1 (<strong>Location</strong>) </p>
  
  <p><strong>Generated tables and context</strong></p>
  
  <pre><code>public partial class Story
  {
      public Story()
      {
          StoryLocation = new HashSet&lt;StoryLocation&gt;();
      }
  
      [Column("id", TypeName = "bigint(20)")]
      public long Id { get; set; }
  
      [InverseProperty("Story")]
      public ICollection&lt;StoryLocation&gt; StoryLocation { get; set; }
  }
  
  public partial class Location
  {
      public Location()
      {
          StoryLocation = new HashSet&lt;StoryLocation&gt;();
      }
  
      [Column("id", TypeName = "bigint(20)")]
      public long Id { get; set; }
  
      [InverseProperty("Location")]
      public ICollection&lt;StoryLocation&gt; StoryLocation { get; set; }
  }
  
  
  
  public partial class StoryLocation
      {
          [Column("story_id", TypeName = "bigint(20)")]
          public long StoryId { get; set; }
          [Column("location_id", TypeName = "bigint(20)")]
          public long LocationId { get; set; }
  
          [ForeignKey("LocationId")]
          [InverseProperty("StoryLocation")]
          public Location Location { get; set; }
          [ForeignKey("StoryId")]
          [InverseProperty("StoryLocation")]
          public Story Story { get; set; }
      }
  </code></pre>
  
  <p><strong>DBContext</strong> and <strong>Fluent Api:</strong></p>
  
  <pre><code>public partial class TreasureContext : DbContext
  {
  
      public virtual DbSet&lt;Location&gt; Locations { get; set; }
      public virtual DbSet&lt;Story&gt; Stories { get; set; }
      public virtual DbSet&lt;StoryLocation&gt; StoryLocations { get; set; }
  
      public TreasureContext(DbContextOptions options):base(options)
      {
  
      }
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          if (!optionsBuilder.IsConfigured)
          {
          optionsBuilder.UseMySql("Server[servername];Port=3306;
                          Database[dbname];Uid=[admin];Pwd=[pwd];");
          }
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Location&gt;(entity =&gt;
          {
              entity.HasIndex(e =&gt; e.ActivityId)
                  .HasName("FKh3ry7cjb7l4vv0qx8d2jbcrcx");
          });
  
  
  
       modelBuilder.Entity&lt;StoryLocation&gt;(entity =&gt;
          {
              entity.HasKey(e =&gt; new { e.LocationId, e.StoryId });
  
              entity.HasIndex(e =&gt; e.StoryId)
                  .HasName("FK_story");
  
              entity.HasOne(d =&gt; d.Location)
                  .WithMany(p =&gt; p.StoryLocation)
                  .HasForeignKey(d =&gt; d.LocationId)
                  .HasConstraintName("FK_location");
  
              entity.HasOne(d =&gt; d.Story)
                  .WithMany(p =&gt; p.StoryLocation)
                  .HasForeignKey(d =&gt; d.StoryId)
                  .HasConstraintName("FK_story");
          });
  
      }
  }
  </code></pre>
  
  <p>ASP MVC Action and wrapper argument:</p>
  
  <pre><code>public class Relation
  {
      public long storyid{get;set;}
      public List&lt;Location&gt;locations;
  }
  
  [HttpPost, ActionName("Attach")]
  public IActionResult Attach([FromBody]Relation relation)
  {
      Story targetStory=this.context.Stories.Find(relation.storyid);
      if(targetStory!=null)
      {
          this.context.StoryLocations.RemoveRange(
              from slocation in this.context.StoryLocations 
              where slocation.StoryId.Equals(targetStory.Id)
              select slocation
              );
  
          foreach(Location loc in relation.locations)
          {
              this.context.StoryLocations.Add(new 
              StoryLocation{Location=loc,Story=targetStory});
          }
          this.context.SaveChanges();
      }
      return RedirectToAction("Index", "Story");
  }
  </code></pre>
  
  <p>I want to attach all Locations received in the ASP MVC Action argument to the story with the storyid also in the argument.</p>