コード例 #1
0
        public Owner CreateOwner(Owner owner)
        {
            var changeTracker = _context.ChangeTracker.Entries <Pet>();

            _context.Attach(owner).State = EntityState.Added;
            _context.SaveChanges();
            return(owner);
        }
コード例 #2
0
        public Owner CreateOwner(Owner owner)
        {
            var own = _ctx.Attach(owner).State = EntityState.Added;

            _ctx.Entry(owner).Collection(o => o.Pets).IsModified = true;
            _ctx.SaveChanges();
            return(owner);
        }
コード例 #3
0
        public Pet CreatePet(Pet pet)
        {
            if (pet != null)
            {
                _context.Attach(pet).State = EntityState.Added;
                _context.Entry(pet).Collection(p => p.ownersHistory).IsModified = true;
            }
            var petSaved = _context.Pets.Add(pet).Entity;

            _context.SaveChanges();
            return(petSaved);
        }
コード例 #4
0
        public Pet CreatePet(Pet pet)
        {
            if (pet.PreviousOwner != null &&
                _ctx.ChangeTracker.Entries <Owner>()
                .FirstOrDefault(ce => ce.Entity.Id == pet.PreviousOwner.Id) == null)
            {
                _ctx.Attach(pet.PreviousOwner);
            }

            var petSaved = _ctx.Pets.Add(pet).Entity;

            _ctx.SaveChanges();
            return(petSaved);
        }
コード例 #5
0
ファイル: PetRepo.cs プロジェクト: KennethRPedersen/PetShop
        /// <summary>
        /// Adds pet to DB.
        /// </summary>
        /// <param name="pet"></param>
        /// <returns>Pet with id from DB.</returns>
        public Pet CreatePet(Pet pet)
        {
            //var changeTracker = _ctx.ChangeTracker.Entries<Owner>();
            try
            {
                _ctx.Attach(pet).State = EntityState.Added;
                _ctx.SaveChanges();
            }
            catch (Exception e)
            {
                throw new Exception("Failed to create pet!");
            }

            return(pet);
        }
コード例 #6
0
        public Owner Create(Owner owner)
        {
            Owner OwnerToReturn = _context.Attach(owner).Entity;

            _context.SaveChanges();
            return(OwnerToReturn);
        }
コード例 #7
0
 public Pet CreatePet(Pet pet)
 {
     /* if (pet.Owner != null &&
      *   _ctx.ChangeTracker.Entries<Owner>()
      *       .FirstOrDefault(ce => ce.Entity.Id == pet.Owner.Id) == null)
      * {
      *   _ctx.Attach(pet.Owner);
      * }
      *
      * var newPet = _ctx.Add(pet).Entity;
      * _ctx.SaveChanges();
      * return newPet;
      */
     _ctx.Attach(pet).State = EntityState.Added;
     _ctx.SaveChanges();
     return(pet);
 }
コード例 #8
0
 public User Update(User userUpdated)
 {
     _ctx.Attach(userUpdated).State = EntityState.Modified;
     _ctx.Remove(_ctx.Users.Where(u => u.Id == userUpdated.Id));
     _ctx.Entry(userUpdated).Reference(u => u.Owner).IsModified = true;
     _ctx.SaveChanges();
     return(userUpdated);
 }
コード例 #9
0
 public Pet Create(Pet pet)
 {
     _context.Attach(pet).State = EntityState.Added;
     _context.SaveChanges();
     return(pet);
     //var petToCreate = _context.Pets.Add(pet).Entity;
     //_context.SaveChanges();
     //return petToCreate;
 }
コード例 #10
0
        public Color CreateColor(Color color)
        {
            var existingColor = _ctx.Colors.FirstOrDefault(c => c.PetColor == color.PetColor);

            if (existingColor != null)
            {
                throw new Exception($"The color you have added does already exist and has the Id: {existingColor.Id}");
            }
            _ctx.Attach(color).State = EntityState.Added;
            _ctx.SaveChanges();
            return(color);
        }
コード例 #11
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);
        }
コード例 #12
0
 public void Edit(User entity)
 {
     _context.Attach(entity).State = EntityState.Modified;
     _context.SaveChanges();
 }
コード例 #13
0
 public Pet Create(Pet pet)
 {
     _ctx.Attach(pet).State = EntityState.Added;
     _ctx.SaveChanges();
     return(pet);
 }
コード例 #14
0
 public Pet AddPet(Pet pet)
 {
     _context.Attach(pet).State = EntityState.Added;
     _context.SaveChanges();
     return(pet);
 }
 public Owner AddOwner(Owner owner)
 {
     _context.Attach(owner).State = EntityState.Added;
     _context.SaveChanges();
     return(owner);
 }
コード例 #16
0
 public Owner UpdateOwner(Owner updateOwner)
 {
     _ctx.Attach(updateOwner).State = EntityState.Modified;
     _ctx.SaveChanges();
     return(updateOwner);
 }
コード例 #17
0
 public void UpdateUser(User user)
 {
     _ctx.Attach(user).State = EntityState.Modified;
     _ctx.SaveChanges();
 }
コード例 #18
0
 public Pet CreateNewPetRepo(Pet newPet)
 {
     _ctx.Attach(newPet).State = EntityState.Added;
     _ctx.SaveChanges();
     return(newPet);
 }
コード例 #19
0
 /// <summary>
 /// Adds the owner to the DB.
 /// </summary>
 /// <param name="owner"></param>
 /// <returns>Owner with id given by DB.</returns>
 public Owner CreateOwner(Owner owner)
 {
     _ctx.Attach(owner).State = EntityState.Added;
     _ctx.SaveChanges();
     return(owner);
 }