当我的代码执行时,我收到此错误。
CurrentValues不能用于处于已删除状态的实体
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();
}
}
我正在删除数据并以上述方式更新数据。我从这个SO链接https://stackoverflow.com/a/27177623/728750
获得了这个概念这下面抛出错误的代码行
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);
}
我在这里犯了什么样的错误。请纠正我。
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; }
}
感谢