private void UpdatePerson(PersonDbContext context, Person personWithNewData, Person personInDb) { context.Entry <Person>(personInDb).CurrentValues.SetValues(personWithNewData); var contactsInDb = personInDb.Contacts.ToList(); foreach (var contactInDb in contactsInDb) { var contact = personWithNewData.Contacts.SingleOrDefault(i => i.Id == contactInDb.Id); if (contact != null) { context.Entry(contactInDb).CurrentValues.SetValues(contact); } else { context.Contacts.Remove(contactInDb); } } foreach (var contact in personWithNewData.Contacts) { if (!personInDb.Contacts.Any(c => c.Id == contact.Id)) { contact.Person = personInDb; personInDb.Contacts.Add(contact); } } }
//public SQLitePersonRepository(IKeyGenerator<long> personKeyGenerator, IKeyGenerator<string> contactKeyGenerator) //{ // _personKeyGenerator = personKeyGenerator; // _contactKeyGenerator = contactKeyGenerator; //} public List <Person> GetAllPeople() { using (var context = new PersonDbContext()) { var people = (from p in context.People.Include(p => p.Contacts) select p).ToList(); return(people); } }
public void DeletePerson(Person person) { using (var context = new PersonDbContext()) { var personInDb = context.People.SingleOrDefault(p => p.Id == person.Id); if (personInDb != null) { context.People.Remove(personInDb); context.SaveChanges(); } } }
public void SavePerson(Person person) { person = SetIdsForNewEntities(person); using (var context = new PersonDbContext()) { var personInDb = context.People.Include(p => p.Contacts).SingleOrDefault(p => p.Id == person.Id); if (personInDb == null) { InsertPerson(context, person); } else { UpdatePerson(context, person, personInDb); } context.SaveChanges(); } }
private void InsertPerson(PersonDbContext context, Person person) { context.People.Add(person); }