These are models:
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; }
}
On my details page user is able to add more emails, delete exsting...etc...or change existing one. How to update collection? I tries this:
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;
String properties are updated nicely, I tried updating collection the same way, but I get duplicate values. How to update emails?
First delete removed emails:
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;
}
}
After that update edited emails:
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;
}
}
And finally by calling db.SaveChanges()
new emails with Id of 0
Will be inserted and you can repeat this cycle for Phones
.
For child entites - easiest way is to delete them all and add new.
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();
Use this generic sub to mark the child state, easy to use
Notes:
- PromatCon: the entity object
- amList: is the child list that you want to add or modify
- rList: is the child list that you want to remove
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.