public async Task <IActionResult> EndRental(int id) { var rental = _context.Rentals.Where(r => r.Id == id).First(); if (id != rental.Id) { return(BadRequest()); } _context.Entry(rental).State = EntityState.Modified; try { CostCalculator costCalculator = new CostCalculator(); rental.RentalEnd = DateTime.Now; rental.TotalCost = costCalculator.CalculateTotalCosts(rental); rental.Paid = false; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RentalExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private static void AddUserStateTest() { var user = new User { FirstName = "Marcin", LastName = $"N_{DateTime.Now.Hour}_{DateTime.Now.Minute}", Discount = 40, PhoneNumber = "123456789" }; using (var dbContext = new BikeRentalContext()) { try { // Add or update - own: // dbContext.Users.Any(u => u == user); // only bool // // performance best: Find(). dbContext.Entry(user); // State: Detached. dbContext.Users.Add(user); // Add user to context. dbContext.Entry(user); // State: Added. dbContext.SaveChanges(); // Save context changes. dbContext.Entry(user); // State: Unchanged. dbContext.Users.Remove(user); // Remove user. dbContext.Entry(user); // State: Deleted. dbContext.SaveChanges(); // Save context changes. dbContext.Entry(user); // State: Detached. } catch (Exception exception) { throw; } } }
public async Task <IActionResult> PutRental(int id, Rental rental) { if (id != rental.ID) { return(BadRequest()); } _context.Entry(rental).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RentalExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> EndRental(int id, Rental rental) { if (id != rental.RentalId) { return(BadRequest()); } if (rental.RentalEnd != null) { return(BadRequest()); } _context.Entry(rental).State = EntityState.Modified; try { rental.RentalEnd = DateTime.Now; rental.TotalCost = CalcTotalCost(rental); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RentalExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private static void DeleteUserTest() { using (var context = new BikeRentalContext()) { var user = context.Users.Find(1); WriteLine(context.Entry(user).State); context.Users.Remove(user); WriteLine(context.Entry(user).State); context.SaveChanges(); } }
public async Task <IActionResult> PutCustomer(int id, Customer customer) { if (id != customer.ID) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private static void AddUserAndCompareStateTest() { using (var context = new BikeRentalContext()) { var user = new User { FirstName = "!!!", LastName = "???" }; WriteLine(context.Entry(user).State); context.Users.Add(user); WriteLine(context.Entry(user).State); context.SaveChanges(); } }
public async Task <IActionResult> PutBikesReserved(int id, BikesReserved bikesReserved) { if (id != bikesReserved.Id) { return(BadRequest()); } _context.Entry(bikesReserved).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BikesReservedExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutEmployee(int id, [FromBody] Employee employee) { if (id != employee.Id) { return(BadRequest()); } _context.Entry(employee).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutShop(int id, Shop shop) { if (id != shop.ShopId) { return(BadRequest()); } _context.Entry(shop).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShopExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutPaymentStatus(int id, PaymentStatus paymentStatus) { if (id != paymentStatus.PaymentStatusId) { return(BadRequest()); } _context.Entry(paymentStatus).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PaymentStatusExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutAccessories(int id, Accessories accessories) { if (id != accessories.Id) { return(BadRequest()); } _context.Entry(accessories).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AccessoriesExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private static void MockWebService() { using (var dbContext = new BikeRentalContext()) { try { var bike = dbContext.Vehicles.AsNoTracking().First(b => b.VehicleId == 1); // WebService send, response, deserialize. var receivedBike = new Bike { VehicleId = 1, IsActive = bike.IsActive, Color = "Green", Number = bike.Number, Type = (bike as Bike).Type }; // This will add new. // dbContext.Bikes.Add(receivedBike); // State: Added. dbContext.Entry(receivedBike); // State: Detached. dbContext.Vehicles.Attach(receivedBike); // Attach to context (only when not tracking). dbContext.Entry(receivedBike); // State: Unchanged. // Whole object is updated. // dbContext.Entry(receivedBike).State = EntityState.Modified; // Only property is updated. dbContext.Entry(receivedBike).Property(p => p.Color).IsModified = true; dbContext.Entry(receivedBike); // State: Modified. dbContext.SaveChanges(); //// Library for nested, duplicated objects when using i.e.: WebServices (attach nested objects). //// GraphDiff //// context.UpdateGraph(company, map => map.OwnedCollection(p => p.Contacts)); } catch (Exception exception) { throw; } } }
private static void UpdateParametersTest() { using (var context = new BikeRentalContext()) { var user = context.Users.Find(25); user.Parameters = new Parameters { P1 = DateTime.Now.Minute, P2 = DateTime.Now.Millisecond }; context.Entry(user).State = EntityState.Modified; // Property is ignored - force modification. context.SaveChanges(); } }
// Change states tracking example. private static void ChangeBikeColor() { using (var dbContext = new BikeRentalContext()) { const string NewColor = "Red"; var bike = dbContext.Vehicles.First(b => b.Color != NewColor); var user = dbContext.Users.Find(1); bike.Color = NewColor; bike.Number = "B1999"; // Override tracked object state - can be changed to i.e. create copy (Added). dbContext.Entry(bike).State = EntityState.Unchanged; // All operating objects (entries). dbContext.ChangeTracker.Entries(); // Stop tracking for performance upgrade - only for non-modify queries. dbContext.Vehicles.Where(b => b.Color.Contains("Red")).AsNoTracking(); dbContext.SaveChanges(); } }
private static void AttachBikeTest() { using (var context = new BikeRentalContext()) { // Either use AsNoTracking or Detach later. Otherwise these bike and bike2 will collide when attaching var bike = context.Bikes.AsNoTracking().First(x => x.VehicleId == 1); // deserialization simulation, similar to App -> WS -> App -> WS var bike2 = new Bike { VehicleId = bike.VehicleId, BikeType = bike.BikeType, Color = bike.Color, IsActive = bike.IsActive, Number = bike.Number }; // Changing deserialized object bike2.Number = "X123"; WriteLine(context.Entry(bike2).State); context.Bikes.Attach(bike2); WriteLine(context.Entry(bike2).State); // This would be not enough here. It is still only in state Unchanged. Setting state to Modified. context.Entry(bike2).State = EntityState.Modified; WriteLine(context.Entry(bike2).State); // Forcing only "Number" property to be treated as modified. This results in better SQL code generation. // Another way is to use EntityFramework.Extended context.Entry(bike2).State = EntityState.Unchanged; context.Entry(bike2).Property(x => x.Number).IsModified = true; WriteLine(context.Entry(bike2).State); context.SaveChanges(); } }