Example #1
0
        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;
            }
        }
Example #2
0
 public T Read(int id)
 {
     using (var db = new PersonAppContext())
     {
         return(Read(db, id));
     }
 }
Example #3
0
 public List <T> Read()
 {
     using (var db = new PersonAppContext())
     {
         return(Read(db));
     }
 }
Example #4
0
 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));
 }
Example #5
0
 public T Update(T t)
 {
     using (var db = new PersonAppContext())
     {
         Update(db, t);
         db.SaveChanges();
         return(t);
     }
 }
Example #6
0
 public T Create(T t)
 {
     using (var db = new PersonAppContext())
     {
         var added = Create(db, t);
         db.SaveChanges();
         return(added);
     }
 }
Example #7
0
 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);
     }
 }
Example #8
0
 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());
 }
Example #10
0
 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;
 }
Example #12
0
 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;
        }
Example #14
0
 protected abstract T Read(PersonAppContext db, int id);
Example #15
0
        protected override void Delete(PersonAppContext db, int id)
        {
            var foundEntity = Read(db, id);

            db.Entry(foundEntity).State = EntityState.Deleted;
        }
Example #16
0
 protected abstract T Create(PersonAppContext db, T t);
 protected override PersonStatus Read(PersonAppContext db, int id)
 {
     return(db.PersonStatuses.FirstOrDefault(x => x.Id == id));
 }
Example #18
0
 protected override Wish Create(PersonAppContext db, Wish t)
 {
     return(db.Wishes.Add(t));
 }
Example #19
0
 protected abstract void Delete(PersonAppContext db, int id);
Example #20
0
 protected override Wish Read(PersonAppContext db, int id)
 {
     return(db.Wishes.FirstOrDefault(x => x.Id == id));
 }
Example #21
0
 protected override void Delete(PersonAppContext db, int id)
 {
     db.Entry(db.Wishes.FirstOrDefault(x => x.Id == id)).State = EntityState.Deleted;
 }
Example #22
0
 protected override void Update(PersonAppContext db, Wish t)
 {
     db.Entry(t).State = EntityState.Modified;
 }
Example #23
0
 protected override List <Wish> Read(PersonAppContext db)
 {
     return(db.Wishes.ToList());
 }
Example #24
0
 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));
 }