如何在实体框架中更新子列表?

时间:2014-09-14 09:37:27

标签: c# asp.net-mvc entity-framework

这些是模特:

public class Contact
{
    public Contact() 
    {
        Emails = new List<Email>(); 
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Address { get; set; }
    public ICollection<Email> Emails { get; set; }

}
 public  class Email
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual int ContactId { get; set; }
}

在我的详细信息页面上,用户可以添加更多电子邮件,删除exsting ...等...或更改现有的电子邮件。 如何更新收藏? 我试着这个:

    var contactFromDb = db.Contacts.Include("Emails").Include("Telephones").Where(x => x.Id == contact.Id).FirstOrDefault();

      contactFromDb.Address = contact.Address;
      contactFromDb.Name = contact.Name;
      contactFromDb.Surname = contact.Surname;
      contactFromDb.Telephones = contact.Telephones;
      contactFromDb.Emails = contact.Emails;

字符串属性很好地更新,我尝试以相同的方式更新集合,但我得到重复的值。如何更新电子邮件?

3 个答案:

答案 0 :(得分:4)

首先删除已删除的电子邮件:

var contactFromDb = db.Contacts.Include("Emails").Include("Telephones").FirstOrDefault(x => x.Id == updatedContact.Id);
contactFromDb.Address = updatedContact.Address;
contactFromDb.Name = updatedContact.Name;
contactFromDb.Surname = updatedContact.Surname;

// Delete removed emails
var emailsToDelete = (from email in contactFromDb.Emails
                        let item = updatedContact.Emails.SingleOrDefault(i => i.Id == email.Id)
                        where item == null
                        select email).ToList();
if (emailsToDelete.Any())
{
    foreach (var email in emailsToDelete)
    {
        db.Entry(email).State = EntityState.Deleted;
    }
}

更新后编辑的电子邮件:

foreach (var email in updatedContact.Emails)
{
    // If id of an email is not equal to 0, it's not new email and it should be updated
    if (email.Id > 0)
    {
        var emailInDb = contactFromDb.Emails.Single(e => e.Id == email.Id);
        db.Entry(emailInDb).CurrentValues.SetValues(email);
        db.Entry(emailInDb).State = EntityState.Modified;
    }
}

最后,通过调用db.SaveChanges()来发送ID为0的新电子邮件,我们会插入Phones

答案 1 :(得分:1)

对于子实体 - 最简单的方法是删除所有实体并添加新实体。

var contactFromDb = db.Contacts.Include("Emails").Include("Telephones").Where(x => x.Id == contact.Id).FirstOrDefault();

contactFromDb.Address = contact.Address;
contactFromDb.Name = contact.Name;
contactFromDb.Surname = contact.Surname;
contactFromDb.Telephones = contact.Telephones;
db.Entry(contactFromDb).State = System.Data.EntityState.Modified;
foreach(Email email in contactFromDb.Emails)
{
    db.Entry(email).State = System.Data.EntityState.Deleted;
}
contactFromDb.Emails.AddRange(contact.Emails);
db.SaveChanges();

答案 2 :(得分:0)

使用此通用子标记子状态,易于使用

  

注意:

     
      
  • PromatCon:实体对象
  •   
  • amList:是您要添加或修改的子列表
  •   
  • rList:是您要删除的子列表
  •   
updatechild(objCas.ECC_Decision, PromatCon.ECC_Decision.Where(c => c.rid == objCas.rid & !objCas.ECC_Decision.Select(x => x.dcid).Contains(c.dcid)).toList())
public void updatechild<Ety>(ICollection<Ety> amList, ICollection<Ety> rList)
{
    if (amList != null)
    {
        foreach (var obj in amList)
        {
            var x = PromatCon.Entry(obj).GetDatabaseValues();
            if (x == null)
                PromatCon.Entry(obj).State = EntityState.Added;
            else
                PromatCon.Entry(obj).State = EntityState.Modified;
        }
    }

    if (rList != null)
    {
        foreach (var obj in rList.ToList())
            PromatCon.Entry(obj).State = EntityState.Deleted;
    }
}
PromatCon.SaveChanges()