void NormalizeContextForUpdate(FilmsContext context, params Film[] films) { try { context.Configuration.AutoDetectChangesEnabled = false; foreach (var film in films) { var newGenres = new List <Genre>(); newGenres.AddRange(film.Genres); film.Genres.Clear(); context.Films.Attach(film); if (film.FilmId != default(long) && context.Films.Any(f => f.FilmId == film.FilmId)) { context.Entry(film).Collection(f => f.Genres).Load(); context.Entry(film).State = EntityState.Modified; } else { context.Entry(film).State = EntityState.Added; } foreach (var genre in newGenres) { if (!film.Genres.Any(g => g.GenreId == genre.GenreId || g.Name.Equals(genre.Name, StringComparison.CurrentCultureIgnoreCase))) { context.Genres.Attach(genre); film.Genres.Add(genre); if (!context.Genres.Any(g => g.Name.ToUpper() == genre.Name.ToUpper())) { context.Entry(genre).State = EntityState.Added; } } } var counter = 0; while (counter < film.Genres.Count) { var genre = film.Genres[counter]; if (!newGenres.Any(g => g.GenreId == genre.GenreId || g.Name.Equals(genre.Name, StringComparison.CurrentCultureIgnoreCase))) { film.Genres.Remove(genre); } else { counter++; } } } } catch { throw; } finally { context.ChangeTracker.DetectChanges(); context.Configuration.AutoDetectChangesEnabled = true; } }
public async Task <IActionResult> PutFilmsItem(int id, FilmsItem filmsItem) { if (id != filmsItem.Id) { return(BadRequest()); } _context.Entry(filmsItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FilmsItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutFilm(int id, Film film) { if (id != film.FilmId) { return(BadRequest()); } FilmValid c = new FilmValid(_context, film); if (!c.Valid()) { return(BadRequest("Фільм з такими даними вже існує")); } _context.Entry(film).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FilmExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutDirector(int id, Director director) { if (id != director.DirectorId) { return(BadRequest()); } DirectorValid c = new DirectorValid(_context, director); if (!c.Valid()) { return(BadRequest("Режисер з такими даними вже існує")); } _context.Entry(director).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DirectorExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutCompany(int id, Company company) { if (id != company.CompanyId) { return(BadRequest()); } CompanyValid c = new CompanyValid(_context, company); if (!c.Valid()) { return(BadRequest("Компанія з такою назвою вже існує")); } _context.Entry(company).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CompanyExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutGenre(int id, Genre genre) { if (id != genre.GenreId) { return(BadRequest()); } GenreValid c = new GenreValid(_context, genre); if (!c.Valid()) { return(BadRequest("Жанр з такою назвою вже існує")); } _context.Entry(genre).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GenreExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task UpdateFilmAsync(Film film) { using (var context = new FilmsContext()) { context.Entry(film).State = EntityState.Modified; await context.SaveChangesAsync(); } }
public ActionResult Edit([Bind(Include = "FilmId,FilmName,FilmLength,Director")] Film film) { if (ModelState.IsValid) { db.Entry(film).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(film)); }
public async Task DeleteFilmAsync(int Id) { using (var context = new FilmsContext()) { var film = await context.Films.FirstOrDefaultAsync(f => f.FilmId == Id); context.Entry(film).State = EntityState.Deleted; await context.SaveChangesAsync(); } }
public ActionResult Edit(Films films) { _db.Entry(films).State = EntityState.Modified; _db.SaveChanges(); return(RedirectToAction("Index")); }