title: Entity Framework: CurrentValues cannot be used for entities in the Deleted state
body: <p>when my code is executing then i am getting this error. </p>
  
  <blockquote>
    <p>CurrentValues cannot be used for entities in the Deleted state</p>
  </blockquote>
  
  <h2>here is my code</h2>
  
  <pre><code>private void button3_Click(object sender, EventArgs e)
  {
      Addresses CurrentAddress = null;
      Contacts CurrentContacts = null;
  
      using (var db = new TestDBContext())
      {
          var existingCustomer = db.Customer
          .Include(a =&gt; a.Addresses.Select(x =&gt; x.Contacts))
          .FirstOrDefault(p =&gt; p.CustomerID == 5);
  
          existingCustomer.FirstName = "Test Customer122";
  
          // selecting address
          foreach (var existingAddress in existingCustomer.Addresses.Where(a =&gt; a.AddressID == 5).ToList())
          {
              CurrentAddress = existingAddress;
              //if (existingCustomer.Addresses.Any(c =&gt; c.AddressID == existingAddress.AddressID))
                  db.Addresses.Remove(existingAddress);
          }
  
          Addresses oAdrModel = new Addresses();
          if (CurrentAddress != null)
          {
              oAdrModel.Address1 = "test add2";
              oAdrModel.Address2 = "test add2";
              oAdrModel.SerialNo = 3;
              oAdrModel.IsDefault = true;
              oAdrModel.CustomerID = existingCustomer.CustomerID;
              db.Entry(CurrentAddress).CurrentValues.SetValues(oAdrModel);
          }
          else
          {
              db.Addresses.Add(oAdrModel);
          }
  
          // selecting contacts
          foreach (var existingContacts in existingCustomer.Addresses.SelectMany(a =&gt; a.Contacts.Where(cc=&gt; cc.ContactID==5)))
          {
              CurrentContacts = existingContacts;
              db.Contacts.Remove(CurrentContacts);
          }
  
          Contacts ContactModel = new Contacts();
          if (CurrentContacts != null)
          {
              ContactModel.Phone = "1111111-33";
              ContactModel.Fax = "1-1111111";
              ContactModel.SerialNo = 4;
              ContactModel.IsDefault = true;
              ContactModel.AddressID = CurrentAddress.AddressID; 
              db.Entry(CurrentAddress).CurrentValues.SetValues(oAdrModel);
          }
          else
          {
              db.Contacts.Add(ContactModel);
          }
  
  
          db.SaveChanges();
      }
  }
  </code></pre>
  
  <p>i am removing data and updateing the data the above way. i got the concept from this SO link <a href="https://stackoverflow.com/a/27177623/728750">https://stackoverflow.com/a/27177623/728750</a></p>
  
  <p>this below lines of code throwing the error </p>
  
  <pre><code>        foreach (var existingAddress in existingCustomer.Addresses.Where(a =&gt; a.AddressID == 5).ToList())
          {
              CurrentAddress = existingAddress;
              //if (existingCustomer.Addresses.Any(c =&gt; c.AddressID == existingAddress.AddressID))
                  db.Addresses.Remove(existingAddress);
          }
  </code></pre>
  
  <p>what kind of mistake i have done here. please rectify me.</p>
  
  <h2>Classes relation</h2>
  
  <pre><code>public class CustomerBase
  {
      public int CustomerID { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
  
      [NotMapped]
      public string Address1 { get; set; }
  
      [NotMapped]
      public string Address2 { get; set; }
  
      [NotMapped]
      public string Phone { get; set; }
  
      [NotMapped]
      public string Fax { get; set; }
  
  }
  
  public class Customer : CustomerBase
  {
      public virtual List&lt;Addresses&gt; Addresses { get; set; }
  }
  
  public class Addresses
  {
      [Key]
      public int AddressID { get; set; }
      public string Address1 { get; set; }
      public string Address2 { get; set; }
      public bool IsDefault { get; set; }
      public int SerialNo { get; set; }
      public virtual List&lt;Contacts&gt; Contacts { get; set; }
  
      public int CustomerID { get; set; }
      public virtual Customer Customer { get; set; }
  }
  
  public class Contacts
  {
      [Key]
      public int ContactID { get; set; }
  
      public string Phone { get; set; }
      public string Fax { get; set; }
      public bool IsDefault { get; set; }
      public int SerialNo { get; set; }
  
      public int AddressID { get; set; }
      public virtual Addresses Customer { get; set; } 
  
  }
  </code></pre>
  
  <p>thanks</p>