3

when my code is executing then i am getting this error.

CurrentValues cannot be used for entities in the Deleted state

here is my 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 => a.Addresses.Select(x => x.Contacts))
        .FirstOrDefault(p => p.CustomerID == 5);

        existingCustomer.FirstName = "Test Customer122";

        // selecting address
        foreach (var existingAddress in existingCustomer.Addresses.Where(a => a.AddressID == 5).ToList())
        {
            CurrentAddress = existingAddress;
            //if (existingCustomer.Addresses.Any(c => 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 => a.Contacts.Where(cc=> 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();
    }
}

i am removing data and updateing the data the above way. i got the concept from this SO link https://stackoverflow.com/a/27177623/728750

this below lines of code throwing the error

        foreach (var existingAddress in existingCustomer.Addresses.Where(a => a.AddressID == 5).ToList())
        {
            CurrentAddress = existingAddress;
            //if (existingCustomer.Addresses.Any(c => c.AddressID == existingAddress.AddressID))
                db.Addresses.Remove(existingAddress);
        }

what kind of mistake i have done here. please rectify me.

Classes relation

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<Addresses> 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<Contacts> 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; } 

}

thanks

Community
  • 1
  • 1
Mou
  • 14,747
  • 38
  • 142
  • 263
  • Try removing the address you want to remove from existingCustomer.Addresses first. – sellotape Nov 03 '16 at 18:57
  • I remove the existing customer. Anyone can point out where I made the mistake? Anyone can post bit of code which help me to fix it. – Mou Nov 03 '16 at 20:51
  • You remove the address from db.Addresses, but you need to _first_ remove the address from existingCustomer.Addresses. Your code shows you're not doing that. – sellotape Nov 03 '16 at 20:53

0 Answers0