Esempio n. 1
0
        public Pet Create(Pet pet)
        {
            /*if (pet.Owner != null && _ctx.ChangeTracker.Entries<Pet>().FirstOrDefault(oe => oe.Entity.Id == pet.Owner.Id) == null)
             * {
             *  _ctx.Attach(pet.Owner);
             * }
             * var petEntry = _ctx.Pets.Add(pet).Entity;
             * _ctx.SaveChanges();
             * return petEntry;*/

            _ctx.Attach(pet).State = EntityState.Added;
            _ctx.SaveChanges();
            return(pet);
        }
Esempio n. 2
0
        public Owner Update(Owner ownerUpdate)
        {
            _ctx.Attach(ownerUpdate).State = EntityState.Modified;
            _ctx.Entry(ownerUpdate).Collection(o => o.Pets).IsModified = true;
            var pets = _ctx.Pets.Where(p => p.Owner.Id == ownerUpdate.Id &&
                                       !ownerUpdate.Pets.Exists(po => po.Id == p.Id));

            foreach (var pet in pets)
            {
                pet.Owner = null;
                _ctx.Entry(pet).Reference(p => p.Owner).IsModified = true;
            }
            _ctx.SaveChanges();
            return(ownerUpdate);
        }