public async Task <IActionResult> PutClient(int id, Client client) { if (id != client.Id) { return(BadRequest()); } _context.Entry(client).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ClientExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public void CanInsertInstructorIntoDatabse() { var builder = new DbContextOptionsBuilder(); builder.UseInMemoryDatabase("InstructorDb"); using (var context = new EnrollmentContext(builder.Options)) { var instructor = new Instructor(); context.Instructors.Add(instructor); Assert.AreEqual(EntityState.Added, context.Entry(instructor).State); } }
static void Display_Students_Of_Course_CleanCode() { var context = new EnrollmentContext(); var course = context.Courses .Include("Enrollments") .FirstOrDefault(x => x.CourseId == 4); foreach (var item in course.Enrollments) { context.Entry(item).Reference(x => x.Student).Load(); Console.WriteLine(item.Student.Name); } }
public void UpdateUser(int id, User user) { if (!_context.Users.Any(e => e.Id == id)) { throw new KeyNotFoundException(); } User userInDb = _context.Users.Find(id); userInDb.FirstName = user.FirstName; userInDb.LastName = user.LastName; _context.Entry(userInDb).State = EntityState.Modified; _context.SaveChanges(); }
static void UpdateUsingEntry() { var context = new EnrollmentContext(); var instructor = context.Instructors .Include(i => i.Courses) .SingleOrDefault(x => x.Name.Contains("Shifu")); var course = instructor.Courses[0]; course.Title = "The first course of master shifu"; using (var newContext = new EnrollmentContext()) { newContext.Entry(course).State = EntityState.Modified; newContext.SaveChanges(); } }
public async Task <int> UpdateAsync(T entity, CancellationToken cancellationToken = default) { DbContext.Entry(entity).State = EntityState.Modified; return(await DbContext.SaveChangesAsync(cancellationToken)); }