title: Entity Framework update foreign key context.set
body: <p>I have two tables <code>Lesson</code> and <code>Group</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/lVcXf.png" rel="nofollow"><img src="https://i.stack.imgur.com/lVcXf.png" alt="enter image description here"></a></p>
  
  <p>I have 4 lessons and 2 groups, all lessons are in the first group (<code>grp_Id=1</code>).</p>
  
  <p>Group :</p>
  
  <pre><code>public partial class Group:IEntity
  {
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
          public Group()
          {
              this.Lessons = new HashSet&lt;Lesson&gt;();
          }
  
          public int Id { get; set; }
          public Nullable&lt;int&gt; Ex_Id { get; set; }
          public string Name { get; set; }
          public Nullable&lt;int&gt; Factor { get; set; }
  
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
          public virtual ICollection&lt;Lesson&gt; Lessons { get; set; }
          public EntityState EntityState { get; set; }
  }
  </code></pre>
  
  <p>Lesson:</p>
  
  <pre><code>public partial class Lesson:IEntity
  {
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
          public Lesson()
          {
          }
  
          public int Id { get; set; }
          public Nullable&lt;int&gt; Grp_Id { get; set; }
          public string Name { get; set; }
          public Nullable&lt;int&gt; FromNum { get; set; }
          public Nullable&lt;int&gt; ToNum { get; set; }
          public Nullable&lt;int&gt; CountQuestio { get; set; }
          public Nullable&lt;int&gt; Factor { get; set; }
  
          public virtual Group Group { get; set; }
  
          public EntityState EntityState { get; set; }
  }
  </code></pre>
  
  <p>I use this manual to implement n-tier layer with Entity Framework <a href="https://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework/" rel="nofollow">Link</a></p>
  
  <p>For saving and modifying, I use this method :</p>
  
  <pre><code>public virtual void Update(params T[] items)
  {
      using (var context = new AzmaEntities())
      {
          DbSet&lt;T&gt; dbSet = context.Set&lt;T&gt;();
  
          foreach (T item in items)
          {
              dbSet.Add(item);
  
              foreach(DbEntityEntry&lt;IEntity&gt; entry in context.ChangeTracker.Entries&lt;IEntity&gt;())
              {
                  IEntity entity = entry.Entity;
                  entry.State = GetEntityState(entity.EntityState);
              }
          }
  
          context.SaveChanges();
      }
  } 
  </code></pre>
  
  <p>This is my example code to update foreign key and update lesson; when find lesson with <code>Id=3</code>, change the group and update <code>Grp_Id</code>:</p>
  
  <pre><code>IGroupBLL grpbll = new GroupBLL();
  
  IList&lt;Group&gt; grps = new List&lt;Group&gt;();
  grps = grpbll.GetAllGroupWithLesson();
  
  foreach (Group grp in grps)
  {
      foreach (Lesson lsn in grp.Lessons)
      {
          if(lsn.Id == 3)
          {
              lsn.EntityState = EntityState.Modified;
              lsn.Grp_Id = 2;
              break;
          }
      }
  }
  
  grpbll.UpdateGroup(grps.ToArray());
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>before running <code>grpbll.UpdateGroup(grps.toArray())</code> grp_id change to 2 </p>
  
  <p><a href="https://i.stack.imgur.com/7i1PS.png" rel="nofollow"><img src="https://i.stack.imgur.com/7i1PS.png" alt="enter image description here"></a></p>
  
  <p>after run <code>UpdateGroup</code>, run <code>Update</code> method. In <code>Update</code> method after run below code change grp_Id to 1 (old grp_Id was 1)</p>
  
  <pre><code>DbSet&lt;T&gt; dbSet = context.Set&lt;T&gt;();
  
  foreach (T item in items )
  {
      dbSet.Add(item);
  
      foreach(DbEntityEntry&lt;IEntity&gt; entry in context.ChangeTracker.Entries&lt;IEntity&gt;())
      {
          IEntity entity = entry.Entity;
          entry.State = GetEntityState(entity.EntityState);
      }
  }
  </code></pre>
  
  <p><strong>Question</strong>: </p>
  
  <p>How to update foreign key in <code>lesson</code> ? How to change lesson is in group only change <code>grp_Id</code>?</p>