public IActionResult AddMovies(Movies movie) { if (ModelState.IsValid) { _context.Movies.Add(movie); _context.SaveChanges(); return(View("ViewMovies", _context.Movies)); } return(View("AddMovies")); }
public ActionResult Create([Bind(Include = "ID,Title,Director,Date")] MovieDB movieDB) { if (ModelState.IsValid) { db.MovieDBs.Add(movieDB); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(movieDB)); }
public ActionResult Create([Bind(Include = "ID,Title,Year,Lenght,Director,Genre")] Movie movie) { if (ModelState.IsValid) { movie.UserId = User.Identity.GetUserId(); db.MovieRepo.Insert(movie); db.SaveChanges(); return(RedirectToAction("Index", "Home")); } return(RedirectToAction("Index", "Home")); }
public void CreateMovieWorks() { // arrange (use the context directly - we assume that works) var options = new DbContextOptionsBuilder <MoviesDBContext>() .UseInMemoryDatabase("createmovie_test").Options; using (var db = new MoviesDBContext(options)) { db.Genre.Add(new Genre { Name = "a" }); db.SaveChanges(); } // act (for act, only use the repo, to test it) using (var db = new MoviesDBContext(options)) { var repo = new MovieRepository(db); Movie movie = new Movie { Name = "b" }; repo.CreateMovie(movie, "a"); repo.SaveChanges(); } // assert (for assert, once again use the context directly to verify.) using (var db = new MoviesDBContext(options)) { Movie movie = db.Movie.Include(m => m.Genre).First(m => m.Name == "b"); Assert.Equal("b", movie.Name); Assert.NotNull(movie.Genre); Assert.Equal("a", movie.Genre.Name); Assert.NotEqual(0, movie.Id); // should get some generated id } }
public void CreateMovieWithoutSaveChangesDoesntWork() { // arrange (use the context directly - we assume that works) var options = new DbContextOptionsBuilder <MoviesDBContext>() .UseInMemoryDatabase("createmoviedoesntsavechanges_test").Options; using (var db = new MoviesDBContext(options)) { db.Genre.Add(new Genre { Name = "a" }); db.SaveChanges(); } // act (for act, only use the repo, to test it) using (var db = new MoviesDBContext(options)) { var repo = new MovieRepository(db); Movie movie = new Movie { Name = "b" }; repo.CreateMovie(movie, "a"); // not calling repo.SaveChanges } // assert (for assert, once again use the context directly to verify.) using (var db = new MoviesDBContext(options)) { Movie movie = db.Movie.Include(m => m.Genre).FirstOrDefault(m => m.Name == "b"); Assert.Null(movie); } }
static void ChangeMovie() { using (var db = new MoviesDBContext()) { // get the first movie by name // this entity is tracked. var movie = db.Movie.OrderBy(m => m.Name).First(); // edit it movie.Name = "Solo"; // create (untracked) genre entity var genre = new Genre { Name = "Action/Adventure" }; // begin tracking it as an "added" entity //db.Add(genre); movie.Genre = genre; // the new genre gets added to the db because the movie // referencing it is tracked. // make sure that we are tracking all changes for this object // (if it's untracked, start tracking it) db.Update(movie); // update the database, picking up tracked changes. db.SaveChanges(); } }
public async Task <CreateContributorTypePerMovieResponse> CreateContributorTypesPerMovieAsync(IEnumerable <CreateContributorTypePerMovieRequest> requests) { var response = new CreateContributorTypePerMovieResponse(); var contribTypes = requests.Select(r => new ContribTypeMovie { MovieId = r.MovieId, ContribId = r.ContributorId, ContribTypeId = r.ContribTypeId }); await _context.AddRangeAsync(contribTypes); response.RowsAffected = _context.SaveChanges(); return(await Task.FromResult(response)); }
public bool DeleteGenreById(int id) { var langTextRecords = _context.Langtext.Where(e => e.GenreId == id); _context.Langtext.RemoveRange(langTextRecords); var movieGenreRecords = _context.MovieGenre.Where(e => e.GenreId == id); _context.MovieGenre.RemoveRange(movieGenreRecords); var genreRecord = _context.Genre.FirstOrDefault(e => e.GenreId == id); _context.MovieGenre.RemoveRange(movieGenreRecords); try { _context.SaveChanges(); } catch (Exception) { return(false); } return(true); }
public IActionResult MoviePost(MovieEntry movieEntry) { if (ModelState.IsValid) { // save movie //TempStorage.addMovie(movieEntry); _context.Movies.Add(movieEntry); _context.SaveChanges(); MovieList movies = new MovieList { Movies = _context.Movies.Where(m => m.Title != "Independance Day") }; // go to Movie List return(Redirect("/Home/EditMovie")); } else { return(View("MoviePost")); } }
public void AddMoviesToDB(MoviesDBContext dbContext) { MovieService movieService = new MovieService(); foreach (var movie in movieService.GetMovies()) { MovieViewModel oNew = new MovieViewModel(); oNew.MovieTitle = movie.MovieTitle; oNew.ReleaseYear = movie.ReleaseYear; oNew.Genre = movie.Genre; dbContext.Movies.Add(oNew); dbContext.SaveChanges(); } }
static void EditSomething() { using (var db = new MoviesDBContext(options)) { // this is going to be executed right away Genre actionGenre = db.Genre.FirstOrDefault(g => g.Name == "Action"); // (First throws exceptions, FirstOrDefault just returns null when there's none) if (actionGenre != null) { actionGenre.Name = "Action/Adventure"; // because actionGenre object is tracked, the context sees any changes and applies them. db.SaveChanges(); } } }
public string PostMovie([FromBody] PostMovieRequest message) { db.Movies.Add(new Movie { MovieTitle = message.Name, Description = message.Description, ReleaseDate = message.ReleaseDate, Actors = message.Actors.Select(x => new Models.Actor { MovieActor = x.Name }).ToList(), Genres = message.Genres.Select(x => new Models.Genre { MovieGenre = x.Name }).ToList(), }); db.SaveChanges(); return("Posted"); }
static void AddAMovie() { using (var db = new MoviesDBContext(options)) { // get first alphabetical name // (no network access yet!) var firstMovieQuery = db.Movie.Include(m => m.Genre).OrderBy(m => m.Name); Movie movie = firstMovieQuery.First(); // that fetched only the one movie, not all of them var newMovie = new Movie { Name = movie.Name + " 2", Genre = movie.Genre }; // few different ways to add this. (aka track the entity) db.Add(newMovie); // this one guesses which DbSet based on the type. //db.Movie.Add(newMovie); // we can add this movie to the genre's navigation property "Movie" collection. //movie.Genre.Movie.Add(newMovie); // after doing any of those, the change to the database is recorded in the DbContext // but not yet applied to the DB. // SaveChanges applies all the tracked changes in one transaction db.SaveChanges(); // now, those extra properties on the newMovie that we didn't set (like Id) // have been filled in by the dbcontext. // (you can continue working and savechanges multiple times on one dbcontext) } // objects you get out of the DbSets are "tracked" by the context. // any changes you make to them will be noticed and applied to the DB // on the next SaveChanges. // objects you create yourself are NOT tracked - track them with db.Add, // db.Update, or adding them to a tracked entity's navigation property. }
static void EarlierCode() { PrintMovies(); ChangeMovie(); PrintMovies(); var movie = new Movie { Id = 1, Name = "Toy Story", GenreId = 1 }; // at this point, movie is untracked. using (var db = new MoviesDBContext()) { // begin tracking the movie, based on the Id it has. // treat any difference as updates to be made. db.Update(movie); // push the changes to the db db.SaveChanges(); } }
public ActionResult Post(Profile user) { try { if (_context.profile.Contains(user)) { _context.profile.Update(user); } else { user.id = null; _context.profile.Add(user); } _context.SaveChanges(); return(Ok(user)); } catch (Exception ex) { return(NotFound("We have some issue" + ex.Message)); } }