/// <summary> /// Update an account in the database /// </summary> /// <param name="myAccount">Updated account parameters, you must supply all parameters</param> public void Update(AccountInfo myAccount) { //SqlParameter[] accountParms = GetAccountParameters(); //SqlParameter[] profileParms = GetProfileParameters(); //SetAccountParameters(accountParms, myAccount); //SetProfileParameters(profileParms, myAccount); //using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC)) { // conn.Open(); // using (SqlTransaction trans = conn.BeginTransaction()) { // try { // SQLHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_UPDATE_ACCOUNT, accountParms); // SQLHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_UPDATE_PROFILE, profileParms); // trans.Commit(); // }catch { // trans.Rollback(); // throw; // } // } //} Account account = new Account { UserId = myAccount.UserId, Email = myAccount.Email, FirstName = myAccount.Address.FirstName, LastName = myAccount.Address.LastName, Addr1 = myAccount.Address.Address1, Addr2 = myAccount.Address.Address2, City = myAccount.Address.City, State = myAccount.Address.State, Zip = myAccount.Address.Zip, Country = myAccount.Address.Country, Phone = myAccount.Address.Phone }; db.Entry(account).State = EntityState.Modified; Profile profile = new Profile { UserId = myAccount.UserId, LangPref = myAccount.Language, FavCategory = myAccount.Category, MyListOpt = myAccount.IsShowFavorites ? 1 : 0, BannerOpt = myAccount.IsShowBanners ? 1 : 0 }; db.Entry(profile).State = EntityState.Modified; db.SaveChanges(); }
public Owner UpdateOwner(Owner owner) { _ctx.Entry(owner).State = EntityState.Detached; _ctx.Attach(owner).State = EntityState.Modified; _ctx.SaveChanges(); return(owner); }
public ActionResult Edit(EditPetModel editPetModel) { if (this.ModelState.IsValid) { var db = new PetShopDbContext(); var pet = db.Pets .FirstOrDefault(p => p.Id == editPetModel.Id); var ownerId = this.User.Identity.GetUserId(); pet.Name = editPetModel.Name; pet.Age = editPetModel.Age; pet.AnimalType = editPetModel.AnimalType; pet.Breed = editPetModel.Breed; pet.Price = editPetModel.Price; pet.ImageUrl = editPetModel.ImageUrl; pet.Color = editPetModel.Color; pet.Size = editPetModel.Size; pet.Coat = editPetModel.Coat; pet.Characteristics = editPetModel.Characteristics; db.Entry(pet).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Details", new { id = editPetModel.Id })); } return(View(editPetModel)); }
public async Task UpdatePet(PetDTO pet) { try { PetAccomodation actuallyAccomodation = _context.PetAccomodations.FirstOrDefault(p => p.PetAccomodationId == pet.AccomodationId); PetAccomodation accomodation = _context.PetAccomodations.FirstOrDefault(p => p.PetAccomodationId == pet.AccomodationId); if (accomodation.Available || pet.AccomodationId == actuallyAccomodation.PetAccomodationId) { actuallyAccomodation.Available = true; _context.PetAccomodations.Update(actuallyAccomodation); _context.SaveChanges(); _context.PetsOwner.Update(pet.PetOwner); _context.SaveChanges(); Pet newPet = new Pet() { AccomodationId = pet.AccomodationId == 0 ? accomodation.PetAccomodationId : actuallyAccomodation.PetAccomodationId, Id = pet.Id, Name = pet.Name, PetHealth = pet.PetHealth, PetPhotograph = pet.PetPhotograph }; accomodation.Available = false; if (pet == null) { throw new Exception("Pet não encontrado"); } _context.Entry <Pet>(newPet).State = EntityState.Modified; _context.Entry <PetAccomodation>(accomodation).State = EntityState.Modified; await _context.SaveChangesAsync(); } else { throw new Exception("Alojamento ocupado, favor escolher outro"); } } catch (Exception ex) { throw new Exception("Ocorreu um erro ao tentar deletar"); } }
public Pet UpdatePet(Pet pet) { if (pet.PreviousOwner != null && (_ctx.ChangeTracker.Entries <Pet>().FirstOrDefault(pe => pe.Entity.Id == pet.PreviousOwner.Id)) == null) { _ctx.Attach(pet.PreviousOwner); } _ctx.Entry(pet).Reference(p => p.PreviousOwner).IsModified = true; //var updated = _ctx.Update(pet).Entity; _ctx.Attach(pet).State = EntityState.Modified; _ctx.SaveChanges(); return(pet); }
public Pet EditPet(int id, Pet editedPet) { //var petTypeChangeTracker = _context.ChangeTracker.Entries<PetType>(); //var ChangeTracker = _context.ChangeTracker.Entries<Pet>(); //if (editedPet.PetType != null && _context.ChangeTracker.Entries<PetType>() // .FirstOrDefault(petTypeEntry => petTypeEntry.Entity.OwnerId == editedPet.PetType.OwnerId) == null) //{ // _context.Attach(editedPet.PetType); //} //else //{ // _context.Entry(editedPet).Reference(pet => pet.PetType).IsModified = true; //} //var petOwnerChangeTracker = _context.ChangeTracker.Entries<Owner>(); //if (editedPet.Owner != null && _context.ChangeTracker.Entries<Owner>() // .FirstOrDefault(ownerEntry => ownerEntry.Entity.OwnerId == editedPet.Owner.OwnerId) == null) //{ // _context.Attach(editedPet.Owner); //} //else //{ // _context.Entry(editedPet).Reference(pet => pet.Owner).IsModified = true; //} // _context.Attach(editedPet).State = EntityState.Modified; if (editedPet.Owner != null) { _context.Attach(editedPet.Owner).State = EntityState.Unchanged; } else { _context.Entry(editedPet).Reference(pet => pet.Owner).IsModified = true; } _context.Attach(editedPet.PetType).State = EntityState.Unchanged; _context.Update(editedPet); _context.SaveChanges(); return(editedPet); }