public async Task ExecuteAsync(Model.Person person) { log.DebugFormat("ExecuteAsync: {0}", person); using (var context = new Db.ContactBookContext()) { EntityEntry <DbModel.Person> personEntity = await context.Persons.AddAsync(new DbModel.Person(person)); await context.SaveChangesAsync(); person.Id = personEntity.Entity.Id; log.DebugFormat("person inserted with id={0}. Try insert contacts", person.Id); foreach (Model.Contact c in person.Contacts) { EntityEntry <DbModel.Contact> contactEntity = await context.Contacts.AddAsync(new DbModel.Contact(c)); await context.SaveChangesAsync(); var pc = new DbModel.PersonContact(personEntity.Entity.Id, contactEntity.Entity.Id); await context.PersonContacts.AddAsync(pc); } await context.SaveChangesAsync(); } log.DebugFormat("Successfully insert {0}", person); }
public IList <Model.Person> Execute() { log.Debug("Execute"); IList <Model.Person> book = new List <Model.Person>(); using (var context = new Db.ContactBookContext()) { var personContacts = from pc in context.PersonContacts join c in context.Contacts on pc.ContactId equals c.Id group new { pc, c } by pc.PersonId into contactsGroupedByPersonId select contactsGroupedByPersonId; var personList = from p in context.Persons orderby p.Name join pc in personContacts on p.Id equals pc.First().pc.PersonId into pcList from pcListOrNull in pcList.DefaultIfEmpty() select new { p, pcListOrNull }; foreach (var pl in personList) { var almostPerson = pl.p.ToPersonViewModel(); var contacts = (pl.pcListOrNull == null) ? new List <Model.Contact>() : pl.pcListOrNull.Select(x => new Model.Contact(x.c)).ToList(); var person = almostPerson(contacts); book.Add(person); } } log.Debug("end of execute"); return(book); }
public async Task ExecuteAsync(Model.Person person) { log.DebugFormat("ExecuteAsync with {0}", person); using (var context = new Db.ContactBookContext()) { context.PersonContacts.RemoveRange(context.PersonContacts.Where(x => x.PersonId == person.Id)); await context.SaveChangesAsync(); var contactIds = person.Contacts.Where(c => c.Id.HasValue).Select(x => x.Id.Value).OrderBy(x => x).ToArray(); context.Contacts.RemoveRange(context.Contacts.Where(x => contactIds.Contains(x.Id))); context.Persons.Remove(new DbModel.Person(person)); await context.SaveChangesAsync(); } log.DebugFormat("End of execute"); }
public async Task ExecuteAsync(Model.Person person) { log.DebugFormat("ExecuteAsync: {0}", person); using (var context = new Db.ContactBookContext()) { // update person context.Persons.Update(new DbModel.Person(person)); // update or remove old contacts var toDeleteOrUpdateMap = person.Contacts.Where(c => c.Id.HasValue).ToDictionary(x => x.Id.Value); var dbContactsMap = (from pc in context.PersonContacts where pc.PersonId == person.Id select pc.Contact).ToDictionary(x => x.Id); var toDelete = dbContactsMap.Where(kvp => !toDeleteOrUpdateMap.ContainsKey(kvp.Key)); var toUpdate = toDeleteOrUpdateMap.Where(kvp => dbContactsMap.ContainsKey(kvp.Key)) .ToDictionary(k => k.Key, v => v.Value); context.PersonContacts.RemoveRange(toDelete.Select(x => new DbModel.PersonContact(person.Id.Value, x.Value.Id))); await context.SaveChangesAsync(); // because PersonContact contais foreign key context.Contacts.RemoveRange(toDelete.Select(x => x.Value)); foreach (var v in context.Contacts.Where(c => toUpdate.ContainsKey(c.Id))) { context.Entry(v).State = Microsoft.EntityFrameworkCore.EntityState.Detached; } context.Contacts.UpdateRange(toUpdate.Select(x => new DbModel.Contact(x.Value))); // add new contacts var toAdd = person.Contacts.Where(c => !c.Id.HasValue).Select(c => new DbModel.Contact(c)); foreach (DbModel.Contact newContact in toAdd) { EntityEntry <DbModel.Contact> newContactEntity = await context.Contacts.AddAsync(newContact); await context.SaveChangesAsync(); await context.PersonContacts.AddAsync(new DbModel.PersonContact(person.Id.Value, newContactEntity.Entity.Id)); } await context.SaveChangesAsync(); } log.DebugFormat("end of execute"); }