public Pet Update(Pet pet)
        {
            List <PetColor> allPetColors = _context.PetColors.ToList();

            allPetColors.ForEach(pc =>
            {
                if (!pet.PetColors.Exists(
                        petColor => petColor.ColorId == pc.ColorId))
                {
                    _context.PetColors.Remove(pc);
                }
            });

            pet.PetColors.RemoveAll(
                pc => allPetColors.Exists(
                    petColor => petColor.ColorId == pc.ColorId &&
                    petColor.PetId == pet.Id));

            pet.PetColors.ForEach(pc =>
            {
                _context.PetColors.Add(new PetColor()
                {
                    ColorId = pc.ColorId, PetId = pet.Id
                });
            });
            pet.PetColors = null;

            _context.Attach(pet).State = EntityState.Modified;
            _context.Entry(pet).Reference(p => p.Owner).IsModified = true;

            _context.SaveChanges();
            return(pet);
        }
Exemplo n.º 2
0
 public Owner Update(Owner owner)
 {
     _context.Attach(owner).State = EntityState.Modified;
     _context.Entry(owner).Collection(o => o.Pets).IsModified = true;
     _context.SaveChanges();
     return(owner);
 }
Exemplo n.º 3
0
 public Color Update(Color color)
 {
     _context.Attach(color).State = EntityState.Modified;
     _context.Entry(color).Collection(c => c.PetColors).IsModified = true;
     _context.SaveChanges();
     return(color);
 }