Entity Framework, Many to Many Relation update in C#

Person personInDb = db.Persons.Find(person.PersonID);
person.Addresses = personInDb.Addresses;
person.Addresses.Clear();

foreach (String id in listOfIds)
{
	Address address = db.Addresses.Find(int.Parse(id));
	person.Addresses.Add(address);
}

db.Entry(personInDb).CurrentValues.SetValues(person);
db.SaveChanges();

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top