title: EF Core 5 failing while trying to update a foreign key which is also part of a unique Index
body: <p>Like the title says, I am trying to update a foreign key and getting below error:</p>
  <blockquote>
  <p>The property 'Student.APID' 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', and then associate the dependent with the new principal.</p>
  </blockquote>
  <p>The <code>Student</code> class looks like this:</p>
  <pre><code> public partial class Student
  {
      public Student()
      {
          EntityCodes = new HashSet&lt;EntityCode&gt;();
          StudentAPHists = new HashSet&lt;StudentAPHist&gt;();
      }
  
      public string DepartmentJurName { get; set; }
      public decimal StudentMBA { get; set; }
      public string Comments { get; set; }
      public bool Active { get; set; }
      public DateTime? dtLastVerified { get; set; }
      public string LastVerifiedBy { get; set; }
      public DateTime? dtCreated { get; set; }
      public string CreatedBy { get; set; }
      public DateTime? StudentVerificationExpiryDate { get; set; }
      public int APID { get; set; }
      public int? DTPID { get; set; }
      public int StudentID { get; set; }
  
      public virtual StudentPayee StudentPayee { get; set; }
      public virtual DelqStudentPayee DelqStudentPayee { get; set; }
      public virtual Department Department { get; set; }
      public virtual ICollection&lt;EntityCode&gt; EntityCodes { get; set; }
      public virtual ICollection&lt;StudentAPHist&gt; StudentAPHists { get; set; }
  }
  </code></pre>
  <p>The <code>StudentPayee</code> looks like this:</p>
  <pre><code>public partial class StudentPayee
  {
      public StudentPayee()
      {
          StudentPayeeUpdateHists = new HashSet&lt;StudentPayeeUpdateHist&gt;();
          Students = new HashSet&lt;Student&gt;();
      }
  
      public decimal StudentMBA { get; set; }
      public decimal PayeeCode { get; set; }
      public string TaxIDMask { get; set; }
      public bool? Active { get; set; }
      public int APID { get; set; }
      public decimal UserRecipient { get; set; }
      public decimal PmtRecipient { get; set; }
      public DateTime dtCreate { get; set; }
      public string CreatedBy { get; set; }
      public string LastUpdatedBy { get; set; }
      public virtual Student Student { get; set; }
      public virtual Payee Payee { get; set; }
      public virtual Recipient PmtRecipientNavigation { get; set; }
      public virtual Recipient UserRecipientNavigation { get; set; }
      public virtual ICollection&lt;StudentPayeeUpdateHist&gt; StudentPayeeUpdateHists { get; set; }
      public virtual ICollection&lt;Student&gt; Students { get; set; }
  }
  </code></pre>
  <p>And the <code>OnModelCreating</code> method looks like this:</p>
  <pre><code>modelBuilder.Entity&lt;Student&gt;(entity =&gt;
          {
              entity.ToTable(&quot;Student&quot;, &quot;dbo&quot;);
  
              entity.HasIndex(e =&gt; new { e.APID, e.StudentMBA }, &quot;UNQ_APID_StudentMBA&quot;)
                  .IsUnique();
  
              entity.Property(e =&gt; e.Comments)
                  .HasMaxLength(256)
                  .IsUnicode(false);
  
              entity.Property(e =&gt; e.DepartmentJurName)
                  .HasMaxLength(50)
                  .IsUnicode(false);
  
              entity.Property(e =&gt; e.CreatedBy)
                  .HasMaxLength(50)
                  .IsUnicode(false);
  
              entity.Property(e =&gt; e.StudentMBA).HasColumnType(&quot;decimal(10, 0)&quot;);
  
              entity.Property(e =&gt; e.LastVerifiedBy)
                  .HasMaxLength(50)
                  .IsUnicode(false);               
  
              entity.Property(e =&gt; e.StudentVerificationExpiryDate).HasColumnType(&quot;datetime&quot;);
  
              entity.Property(e =&gt; e.dtCreated).HasColumnType(&quot;datetime&quot;);
  
              entity.Property(e =&gt; e.dtLastVerified).HasColumnType(&quot;datetime&quot;);
  
              entity.HasOne(d =&gt; d.StudentPayee)
                  .WithMany(p =&gt; p.Students)
                  .HasPrincipalKey(p =&gt; p.APID)
                  .HasForeignKey(d =&gt; d.APID)
                  .HasConstraintName(&quot;FK_APID_StudentPayee&quot;);
  
              entity.HasOne(d =&gt; d.DelqStudentPayee)
                  .WithMany(p =&gt; p.Students)
                  .HasForeignKey(d =&gt; d.DTPID)
                  .OnDelete(DeleteBehavior.SetNull)
                  .HasConstraintName(&quot;FK_Student_Delqpayee&quot;);
  
              entity.HasOne(d =&gt; d.Department)
                  .WithMany(p =&gt; p.Students)
                  .HasForeignKey(d =&gt; d.StudentMBA)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName(&quot;FK_Student_Student&quot;);
          });
  </code></pre>
  <p>Entity config for <code>StudentPayee</code>:</p>
  <pre><code> modelBuilder.Entity&lt;StudentPayee&gt;(entity =&gt;
          {
              entity.HasKey(e =&gt; new { e.StudentMBA, e.PayeeCode })
                  .HasName(&quot;PK_StudentIDMask&quot;);
  
              entity.ToTable(&quot;StudentPayee&quot;, &quot;dbo&quot;);
  
              entity.HasIndex(e =&gt; new { e.PayeeCode, e.StudentMBA }, &quot;NonClusteredIndex-20201218-155947&quot;)
                  .IsUnique();
  
              entity.HasIndex(e =&gt; e.APID, &quot;UNQ_APID&quot;)
                  .IsUnique();
  
              entity.Property(e =&gt; e.StudentMBA).HasColumnType(&quot;decimal(10, 0)&quot;);
  
              entity.Property(e =&gt; e.PayeeCode).HasColumnType(&quot;decimal(10, 0)&quot;);
  
              entity.Property(e =&gt; e.APID).ValueGeneratedOnAdd();
  
              entity.Property(e =&gt; e.Active)
                  .IsRequired()
                  .HasDefaultValueSql(&quot;((1))&quot;);
  
              entity.Property(e =&gt; e.CreatedBy)
                  .IsRequired()
                  .HasMaxLength(50)
                  .IsUnicode(false)
                  .HasDefaultValueSql(&quot;(suser_sname())&quot;);
  
              entity.Property(e =&gt; e.LastUpdatedBy)
                  .IsRequired()
                  .HasMaxLength(50)
                  .IsUnicode(false);
  
              entity.Property(e =&gt; e.PmtRecipient).HasColumnType(&quot;decimal(10, 0)&quot;);
  
              entity.Property(e =&gt; e.UserRecipient).HasColumnType(&quot;decimal(10, 0)&quot;);
  
              entity.Property(e =&gt; e.TaxIDMask)
                  .HasMaxLength(200)
                  .IsUnicode(false);
  
              entity.Property(e =&gt; e.dtCreate)
                  .HasColumnType(&quot;datetime&quot;)
                  .HasDefaultValueSql(&quot;(getdate())&quot;);
  
              entity.HasOne(d =&gt; d.Student)
                  .WithMany(p =&gt; p.StudentPayees)
                  .HasForeignKey(d =&gt; d.StudentMBA)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName(&quot;FK_StudentPayee_Student&quot;);
  
              entity.HasOne(d =&gt; d.Payee)
                  .WithMany(p =&gt; p.StudentPayees)
                  .HasForeignKey(d =&gt; d.PayeeCode)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName(&quot;FK_StudentPayee_Payee&quot;);
  
              entity.HasOne(d =&gt; d.PmtRecipientNavigation)
                  .WithMany(p =&gt; p.StudentPayeePmtRecipientNavigations)
                  .HasForeignKey(d =&gt; d.PmtRecipient)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName(&quot;FK_StudentPayee_PmtRecipient&quot;);
  
              entity.HasOne(d =&gt; d.UserRecipientNavigation)
                  .WithMany(p =&gt; p.StudentPayeeTBRRecipientNavigations)
                  .HasForeignKey(d =&gt; d.TBRRecipient)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName(&quot;FK_StudentPayee_TBRRecipient&quot;);
          });
  </code></pre>
  <p>As you can see the <code>APID</code> is a foreign key and part of the unique index. So, whenever I try to update the <code>APID</code> like below, I get the error. This used to work with Entity Framework 6.</p>
  <pre><code>Student _student = DataBase.Students
                             .Single(x =&gt; x.Student ID == studentId);
  
  if (_student != null)
  {
      _student.APID = p_APID;
  
      DataBase.SaveChanges();
  }
  </code></pre>