Answered by:
Parent child data insert and update issue

Question
-
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 http://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. thanks
Thursday, November 3, 2016 2:42 PM
Answers
-
Hi Mou_inn,
Based on your description, I create a demo and reproduce your issue on side, please modify your code like this:
// 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.Addresses.Add(oAdrModel);
Best regards,
Cole Wu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Marked as answer by Sudip_inn Friday, November 4, 2016 1:31 PM
Friday, November 4, 2016 9:41 AM -
here is my complete code which is working now fine.
using (var db = new TestDBContext()) { //db.Database.Log = s => MyLogger.Log("EFApp", s); var existingCustomer = db.Customer .Include(a => a.Addresses.Select(x => x.Contacts)) .FirstOrDefault(p => p.CustomerID == 5); existingCustomer.FirstName = "Test Customer123"; existingCustomer.Addresses.Where(a => a.AddressID == 5).ToList().ForEach(r => db.Addresses.Remove(r)); existingCustomer.Addresses.Where(a => a.AddressID == 5).SelectMany(ad => ad.Contacts).Where(c=> c.ContactID==5).ToList().ForEach(r => db.Contacts.Remove(r)); Addresses oAdrModel = new Addresses(); oAdrModel.Address1 = "test xxx"; oAdrModel.Address2 = "test xxx"; oAdrModel.SerialNo = 3; oAdrModel.IsDefault = true; oAdrModel.CustomerID = 5; db.Addresses.Add(oAdrModel); db.SaveChanges(); int CurAddressID = oAdrModel.AddressID; Contacts ContactModel = new Contacts(); ContactModel.Phone = "XX-1111111-33"; ContactModel.Fax = "XX-1-1111111"; ContactModel.SerialNo = 4; ContactModel.IsDefault = true; ContactModel.AddressID = CurAddressID; db.Contacts.Add(ContactModel); db.SaveChanges(); }
- Marked as answer by Sudip_inn Friday, November 4, 2016 1:35 PM
Friday, November 4, 2016 1:31 PM
All replies
-
Hi Mou_inn,
Based on your description, I create a demo and reproduce your issue on side, please modify your code like this:
// 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.Addresses.Add(oAdrModel);
Best regards,
Cole Wu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Marked as answer by Sudip_inn Friday, November 4, 2016 1:31 PM
Friday, November 4, 2016 9:41 AM -
here is my complete code which is working now fine.
using (var db = new TestDBContext()) { //db.Database.Log = s => MyLogger.Log("EFApp", s); var existingCustomer = db.Customer .Include(a => a.Addresses.Select(x => x.Contacts)) .FirstOrDefault(p => p.CustomerID == 5); existingCustomer.FirstName = "Test Customer123"; existingCustomer.Addresses.Where(a => a.AddressID == 5).ToList().ForEach(r => db.Addresses.Remove(r)); existingCustomer.Addresses.Where(a => a.AddressID == 5).SelectMany(ad => ad.Contacts).Where(c=> c.ContactID==5).ToList().ForEach(r => db.Contacts.Remove(r)); Addresses oAdrModel = new Addresses(); oAdrModel.Address1 = "test xxx"; oAdrModel.Address2 = "test xxx"; oAdrModel.SerialNo = 3; oAdrModel.IsDefault = true; oAdrModel.CustomerID = 5; db.Addresses.Add(oAdrModel); db.SaveChanges(); int CurAddressID = oAdrModel.AddressID; Contacts ContactModel = new Contacts(); ContactModel.Phone = "XX-1111111-33"; ContactModel.Fax = "XX-1-1111111"; ContactModel.SerialNo = 4; ContactModel.IsDefault = true; ContactModel.AddressID = CurAddressID; db.Contacts.Add(ContactModel); db.SaveChanges(); }
- Marked as answer by Sudip_inn Friday, November 4, 2016 1:35 PM
Friday, November 4, 2016 1:31 PM