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

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