title: Entity Framwork Update many to many
body: <p>I am new to ef core. I am trying to implement many to many .</p>
  <p>Here is my DBContext</p>
  <pre><code>public class MaxCiSDbContext : DbContext
  {
      public DbSet&lt;Job&gt; Jobs { get; set; }
      public DbSet&lt;Staff&gt; Staffs { get; set; }
  
      public MaxCiSDbContext(DbContextOptions&lt;MaxCiSDbContext&gt; options) : base(options)
      {
      }
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Job&gt;()
              .HasMany(t =&gt; t.Staffs)
              .WithMany(t =&gt; t.Jobs);
              
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>
  <p>and Here is my Staff Class</p>
  <pre><code>public class Staff
  {
      public string Id { get; set; }
      public string Name { get; set; }
      public string Email { get; set; }
      public string Phone { get; set; }
      public string Mobile { get; set; }
      public string Address { get; set; }
  
      //Navigation 
      public virtual ICollection&lt;Job&gt; Jobs { get; set; }
  }
  </code></pre>
  <p>Here is my Job Class</p>
  <pre><code>public class Job
  {
      [Key]
      public string Id { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public string State { get; set; }
      public string ClientOrderNumber { get; set; }
      public string StartDate { get; set; }
      public string DueDate { get; set; }
      public string CompletedDate { get; set; }
      public string ClientId { get; set; }
      public string ManagerId { get; set; }
      public string PartnerId { get; set; }
      //Navigation 
      public virtual ICollection &lt;Staff&gt; Staffs { get; set; }
  }
  </code></pre>
  <p>I Call an API which returns a XmlDocument, I read that document and update database.
  Here is how I deal with xmldocument.</p>
  <pre><code>        //Fetch Current Jobs and populate to DB
          XmlDocument apiresults = JobMethods.GetCurrent();
          XmlNodeList nodes = apiresults.DocumentElement.SelectNodes(&quot;/Response/Jobs/Job&quot;);
  
          foreach (XmlNode node in nodes)
          {
              Job MaxCiSJob = new Job()
              {
                  Id = node.SelectSingleNode(&quot;ID&quot;).InnerText,
                  Name = node.SelectSingleNode(&quot;Name&quot;).InnerText,
                  Description = node.SelectSingleNode(&quot;Description&quot;).InnerText,
                  State = node.SelectSingleNode(&quot;State&quot;).InnerText,
                  ClientOrderNumber = node.SelectSingleNode(&quot;ClientOrderNumber&quot;).InnerText,
                  StartDate = node.SelectSingleNode(&quot;StartDate&quot;).InnerText,
                  DueDate = node.SelectSingleNode(&quot;DueDate&quot;) != null ? node.SelectSingleNode(&quot;DueDate&quot;).InnerText : &quot;&quot;,
                  CompletedDate = node.SelectSingleNode(&quot;CompletedDate&quot;) != null ? node.SelectSingleNode(&quot;CompletedDate&quot;).InnerText : &quot;&quot;,
                  ClientId = node.SelectSingleNode(&quot;Client/ID&quot;).InnerText,
                  ManagerId = node.SelectSingleNode(&quot;Manager/ID&quot;) != null ? node.SelectSingleNode(&quot;Manager/ID&quot;).InnerText : &quot;&quot;,
                  PartnerId = node.SelectSingleNode(&quot;Partner&quot;) != null ? node.SelectSingleNode(&quot;Partner&quot;).InnerText : &quot;&quot;
  
  
              };
              XmlNodeList Assigned = node.SelectNodes(&quot;Assigned/Staff&quot;);
              MaxCiSJob.Staffs = new Collection&lt;Staff&gt;();
              foreach (XmlNode staffNode in Assigned)
              {
                  var staffId = staffNode.SelectSingleNode(&quot;ID&quot;).InnerText;
  
                  var staff = _db.Staffs.Find(staffId);
                  if(staff != null)
                  {
                      MaxCiSJob.Staffs.Add(staff);
                  }
  
              }
  
              if (_db.Jobs.Find(MaxCiSJob.Id) == null)
              {
                  //Insert Record
                  _db.Jobs.Add(MaxCiSJob);
              }
              else
              {
                  // UPDATE recorde
                  _db.Jobs.Update(MaxCiSJob);
              }
  
          }
          _db.SaveChanges();
      }
  </code></pre>
  <p>Everything works well when I run the program for the first time(The linking table ,&quot;JobStaff&quot;, is empty) but when I run the Program for the second time I get an excetpion:</p>
  <p>SqlException: Violation of PRIMARY KEY constraint 'PK_JobStaff'. Cannot insert duplicate key in object 'dbo.JobStaff'. The duplicate key value is (J14995, 557898).</p>
  <p>Can someone please help me on how can I resolve this issue.</p>