protected override void Update(PersonAppContext db, Person t) { var existingPerson = db.Persons .Include(x => x.Wishes).FirstOrDefault(s => s.Id == t.Id); if (existingPerson == null) { return; } { var wishesToRemove = new List <Wish>(existingPerson.Wishes.Where(i => t.Wishes.FirstOrDefault(x => x.Id == i.Id) == null)); var wishesToAdd = new List <Wish>(t.Wishes.Where(i => existingPerson.Wishes.FirstOrDefault(x => x.Id == i.Id) == null)); foreach (var wish in wishesToRemove) { existingPerson.Wishes.Remove(wish); } foreach (var wish in wishesToAdd) { db.Wishes.Attach(wish); existingPerson.Wishes.Add(wish); } existingPerson.Name = t.Name; existingPerson.PersonStatusId = t.PersonStatusId; db.Entry(existingPerson).State = EntityState.Modified; } }
public T Read(int id) { using (var db = new PersonAppContext()) { return(Read(db, id)); } }
public List <T> Read() { using (var db = new PersonAppContext()) { return(Read(db)); } }
protected override Person Create(PersonAppContext db, Person t) { foreach (var w in t.Wishes) { db.Entry(w).State = EntityState.Unchanged; } return(db.Persons.Add(t)); }
public T Update(T t) { using (var db = new PersonAppContext()) { Update(db, t); db.SaveChanges(); return(t); } }
public T Create(T t) { using (var db = new PersonAppContext()) { var added = Create(db, t); db.SaveChanges(); return(added); } }
public bool Delete(int id) { using (var db = new PersonAppContext()) { if (Read(db, id) == null) { return(false); } try { Delete(db, id); db.SaveChanges(); } catch (Exception e) { Console.WriteLine(e.StackTrace); } return(Read(db, id) == null); } }
protected override Person Read(PersonAppContext db, int id) { return(db.Persons.Include(x => x.Status).Include(x => x.Wishes).FirstOrDefault(x => x.Id == id)); }
protected override List <PersonStatus> Read(PersonAppContext db) { return(db.PersonStatuses.ToList()); }
protected abstract void Update(PersonAppContext db, T t);
protected override void Update(PersonAppContext db, PersonStatus t) { db.Entry(t).State = System.Data.Entity.EntityState.Modified; }
protected abstract List <T> Read(PersonAppContext db);
protected override void Delete(PersonAppContext db, int id) { var foundEntity = db.PersonStatuses.FirstOrDefault(x => x.Id == id); db.Entry(foundEntity).State = System.Data.Entity.EntityState.Deleted; }
protected abstract T Read(PersonAppContext db, int id);
protected override void Delete(PersonAppContext db, int id) { var foundEntity = Read(db, id); db.Entry(foundEntity).State = EntityState.Deleted; }
protected abstract T Create(PersonAppContext db, T t);
protected override PersonStatus Read(PersonAppContext db, int id) { return(db.PersonStatuses.FirstOrDefault(x => x.Id == id)); }
protected override Wish Create(PersonAppContext db, Wish t) { return(db.Wishes.Add(t)); }
protected abstract void Delete(PersonAppContext db, int id);
protected override Wish Read(PersonAppContext db, int id) { return(db.Wishes.FirstOrDefault(x => x.Id == id)); }
protected override void Delete(PersonAppContext db, int id) { db.Entry(db.Wishes.FirstOrDefault(x => x.Id == id)).State = EntityState.Deleted; }
protected override void Update(PersonAppContext db, Wish t) { db.Entry(t).State = EntityState.Modified; }
protected override List <Wish> Read(PersonAppContext db) { return(db.Wishes.ToList()); }
protected override List <Person> Read(PersonAppContext db) { return(db.Persons.Include(x => x.Status).Include(x => x.Wishes).ToList()); }
protected override PersonStatus Create(PersonAppContext db, PersonStatus t) { return(db.PersonStatuses.Add(t)); }