/// <summary> /// Updates a pet in the DB. /// </summary> /// <param name="id"></param> /// <param name="newPet"></param> public Pet SavePet(int id, Pet newPet) { //if(newPet.Owner != null) //{ // newPet.Owner = _ctx.Owners.FirstOrDefault(x => x.Id == newPet.Owner.Id); //} //else //{ // _ctx.Entry(newPet).Reference(x => x.Owner).IsModified = true; //} //_ctx.Update(newPet); if (!_ctx.Pets.Any(x => x.Id == id)) { throw new ArgumentException($"Could not find any pet with ID {id}!"); } try { _ctx.Attach(newPet).State = EntityState.Modified; _ctx.Entry(newPet).Reference(x => x.Owner).IsModified = true; _ctx.Entry(newPet).Collection(np => np.Colors).IsModified = true; _ctx.SaveChanges(); } catch (Exception e) { throw new Exception("Failed to save pet!"); } return(newPet); }
public User GetUserById(int id) { User user = _ctx.Users.FirstOrDefault(u => u.Id == id); _ctx.Entry(user).State = EntityState.Detached; return(user); }
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); }
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); }
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); }
public Pet UpdatePet(Pet pet) { _context.Attach(pet).State = EntityState.Modified; _context.Entry(pet).Reference(p => p.PreviousOwner).IsModified = true; _context.SaveChanges(); return(pet); }
public Pet Update(Pet petUpdate) { _ctx.Attach(petUpdate).State = EntityState.Modified; _ctx.Entry(petUpdate).Reference(p => p.Owner).IsModified = true; _ctx.SaveChanges(); return(petUpdate); }
public Color UpdateColor(Color color) { _ctx.Colors.Attach(color).State = EntityState.Modified; _ctx.Entry(color).Collection(x => x.Pets).IsModified = true; _ctx.SaveChanges(); return(color); }
public Pet UpdatePetInfoRepo(Pet petUpdate) { _ctx.Attach(petUpdate).State = EntityState.Modified; _ctx.Entry(petUpdate).Reference(o => o.previousOwner).IsModified = true; _ctx.SaveChanges(); return(petUpdate); }
public Owner UpdateOwner(Owner ownerToUpdate) { _context.Attach(ownerToUpdate).State = EntityState.Modified; _context.Entry(ownerToUpdate).Collection(o => o.Pets).IsModified = true; _context.SaveChanges(); return(ownerToUpdate); }
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); }
public ActionResult Edit([Bind(Include = "EmployeeId,Name,Surname,DateOfBirth,EmailAddress,PhoneNumber,CreatedOn,ModifiedOn,TitleId,Salary")] Employee employee) { if (ModelState.IsValid) { db.Entry(employee).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.TitleId = new SelectList(db.Titles, "TitleId", "Name", employee.TitleId); return(View(employee)); }
public ActionResult Edit([Bind(Include = "CustomerId,Name,Surname,DateOfBirth,EmailAddress,PhoneNumber,CreatedOn,ModifiedOn,Bonuses,Street,CityId")] Customer customer) { if (ModelState.IsValid) { db.Entry(customer).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CityId = new SelectList(db.Cities, "CityId", "Name", customer.CityId); return(View(customer)); }
public ActionResult Edit([Bind(Include = "SaleId,CreatedOn,StartDate,EndDate,Percentage,ItemId")] Sale sale) { if (ModelState.IsValid) { sale.ModifiedOn = DateTime.UtcNow; db.Entry(sale).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ItemId = new SelectList(db.Items, "ItemId", "Name", sale.ItemId); return(View(sale)); }
public ActionResult Edit([Bind(Include = "ItemId,Name,Price,Description,Volume")] Item item, string Animal, string Brand, string Subcategory, string Category) { if (ModelState.IsValid) { var animal = db.Animals.Where(a => a.Name.GetHashCode().ToString() == Animal).FirstOrDefault(); if (animal == null) { animal = db.Animals.Add(new Models.Animal() { Name = Animal, CreatedOn = DateTime.UtcNow }); db.SaveChanges(); } var brand = db.Brands.Where(a => a.Name == Brand).FirstOrDefault(); if (brand == null) { brand = db.Brands.Add(new Models.Brand() { Name = Brand, CreatedOn = DateTime.UtcNow }); db.SaveChanges(); } var category = db.ItemCategories.Where(a => a.Name == Category).FirstOrDefault(); if (category == null) { category = db.ItemCategories.Add(new Models.ItemCategory() { Name = Category, CreatedOn = DateTime.UtcNow }); db.SaveChanges(); } var subcategory = db.ItemSubcategories.Where(a => a.Name == Subcategory).FirstOrDefault(); if (subcategory == null) { subcategory = db.ItemSubcategories.Add(new Models.ItemSubcategory() { Name = Category, CreatedOn = DateTime.UtcNow, ItemCategory = category }); db.SaveChanges(); } db.Entry(item).State = EntityState.Modified; item.BrandId = brand.BrandId; item.ItemSubcategory = subcategory; item.ModifiedOn = DateTime.UtcNow; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Animals = db.Animals.Select(a => a.Name).ToList(); ViewBag.Brands = db.Brands.Select(a => a.Name).ToList(); ViewBag.ItemSubcategories = db.ItemSubcategories.Select(a => a.Name).ToList(); ViewBag.Itemcategories = db.ItemCategories.Select(a => a.Name).ToList(); return(View(item)); }
public ActionResult Edit([Bind(Include = "TotalSum,CreatedOn,ModifiedOn,ItemsOrderId,SupplierId,EmployeeId")] ItemsOrder itemsOrder) { if (ModelState.IsValid) { db.Entry(itemsOrder).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.EmployeeId = new SelectList(db.Employees, "EmployeeId", "Name", itemsOrder.EmployeeId); ViewBag.SupplierId = new SelectList(db.Suppliers, "SupplierId", "Name", itemsOrder.SupplierId); return(View(itemsOrder)); }
public ActionResult Edit([Bind(Include = "PurchaseId,CreatedOn,ModifiedOn,TotalSum,EmployeeId,CheckNumber,CustomerId")] Purchase purchase) { if (ModelState.IsValid) { db.Entry(purchase).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "Name", purchase.CustomerId); ViewBag.EmployeeId = new SelectList(db.Employees, "EmployeeId", "Name", purchase.EmployeeId); return(View(purchase)); }
public async Task AddOrEditAnimalAsync(Animal animal) { //Check if animal has got an Id, and if so update it. if (animal.AnimalId != 0) { //Check if animal exists loacally, and if it is detach it, so can update it. Animal local = petShop.Set <Animal>().Local.FirstOrDefault(entry => entry.AnimalId.Equals(animal.AnimalId)); if (local != null) { // detach petShop.Entry(local).State = EntityState.Detached; } // set Modified flag in your entry petShop.Entry(animal).State = EntityState.Modified; } else { await petShop.Animals.AddAsync(animal);; } await petShop.SaveChangesAsync(); }
public Owner UpdateOwner(Owner toBeUpdated, Owner updatedOwner) { _context.Attach(toBeUpdated).State = EntityState.Modified; var petOwners = new List <PetOwner>(toBeUpdated.petHistory ?? new List <PetOwner>()); _context.PetOwners.RemoveRange( _context.PetOwners.Where(po => po.OwnerId == toBeUpdated.id) ); foreach (var po in petOwners) { _context.Entry(po).State = EntityState.Added; } _context.SaveChanges(); return(toBeUpdated); }
public Pet UpdatePet(Pet petToUpdate) { _context.Attach(petToUpdate).State = EntityState.Modified; // _context.Entry(petToUpdate).Collection(p => p.ownersHistory).IsModified = true; var petOwners = new List <PetOwner>(petToUpdate.ownersHistory ?? new List <PetOwner>()); _context.PetOwners.RemoveRange( _context.PetOwners.Where(p => p.PetId == petToUpdate.id) ); foreach (var po in petOwners) { _context.Entry(po).State = EntityState.Added; } _context.SaveChanges(); return(petToUpdate); }
public bool UpdatePet(Pet pet) { if (pet.PreviousOwner != null && _ctx.ChangeTracker.Entries <Owner>() .FirstOrDefault(ce => ce.Entity.Id == pet.PreviousOwner.Id) == null) { _ctx.Attach(pet.PreviousOwner); } else { _ctx.Entry(pet).Reference(p => p.PreviousOwner).IsModified = true; } var petUpdated = _ctx.Pets.Update(pet).Entity; _ctx.SaveChanges(); return(true); }
public Owner UpdateOwner(Owner toBeUpdated) { if (toBeUpdated != null) { _context.Attach(toBeUpdated).State = EntityState.Modified; //_context.Entry(petUpdate).Collection(p => p.ownersHistory).IsModified = true; } var petOwners = new List <PetOwner>(toBeUpdated.petHistory ?? new List <PetOwner>()); _context.PetOwners.RemoveRange( _context.PetOwners.Where(p => p.PetId == toBeUpdated.id) ); foreach (var po in petOwners) { _context.Entry(po).State = EntityState.Added; } _context.SaveChanges(); return(toBeUpdated); }
public void Edit(User entity) { db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); }
/// <summary> /// Updates an Owner in the DB. /// </summary> /// <param name="id"></param> /// <param name="owner"></param> public void SaveOwner(int id, Owner owner) { _ctx.Attach(owner).State = EntityState.Modified; _ctx.Entry(owner).Collection(o => o.Pets).IsModified = true; _ctx.SaveChanges(); }
public User Edit(User entity) { db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); return(entity); }
public void Edit(User entity) { _context.Entry(entity).State = EntityState.Modified; _context.SaveChanges(); }
public void Edit(User entity) { db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified; db.SaveChanges(); }