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);
            }
        }
예제 #3
0
        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();
            }
        }
        static void PrintMovies()
        {
            var movies = new List <Movie>();

            using (var db = new MoviesDBContext(options))
            {
                // at this point, we haven't connected to the db yet

                // this winds up as a "SELECT * FROM Movies.Movie" somewhere
                movies = db.Movie.ToList();
                // this does not fetch the full "entity graph" of the movie -
                // "navigation properties" like Genre are null.

                // this winds up as a SELECT with a join.
                movies = db.Movie.Include(m => m.Genre).ToList();
                // with .Include, we fetch the related properties so they are not null.

                // EF stores the result sets in the DbSets of the DbContext, so
                // this second access will use cached values. (faster)
                movies = db.Movie.Include(m => m.Genre).ToList();
            }

            foreach (var item in movies)
            {
                Console.WriteLine($"movie ID {item.Id}, name {item.Name}, genre {item.Genre.Name}");
            }
        }
예제 #5
0
 static void PrintMovies()
 {
     using (var db = new MoviesDBContext())
     {
         foreach (var item in db.Movie.Include(m => m.Genre))
         {
             Console.WriteLine($"Name {item.Name}," +
                               $" genre {item.Genre.Name}");
         }
     }
 }
예제 #6
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            Database.SetInitializer(new ApplicationMovieContext());
            var dbMovie = new MoviesDBContext();
            //dbMovie.Database.Initialize(true);

            Database.SetInitializer(new ApplicationDbInitializer());
            var db = new ApplicationDbContext();
            db.Database.Initialize(true);
        }
예제 #7
0
 public IEnumerable <Movies> Get()
 {
     using (var _context = new MoviesDBContext())
     {
         /*Movies movie = new Movies();
          * //movie.MoviesId = 1;
          * movie.Name = "First Title";
          * movie.Year = 2000;
          *
          * _context.Movies.Add(movie);
          *
          * _context.SaveChanges();*/
         return(_context.Movies.ToList());
     }
 }
예제 #8
0
        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();
                }
            }
        }
예제 #10
0
        static void AccessWithRepo()
        {
            using (var db = new MoviesDBContext(options))
            {
                IMovieRepository movieRepo = new MovieRepository(db);

                movieRepo.CreateMovie(new Movie {
                    Name = "Harry Potter"
                }, "Action/Adventure");
                movieRepo.SaveChanges();

                foreach (var movie in movieRepo.GetAllMoviesWithGenres())
                {
                    Console.WriteLine(movie.Name);
                }
            }
        }
예제 #11
0
        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.
        }
예제 #12
0
        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 void SaveChangesWithNoChangesDoesntThrowException()
        {
            // arrange
            var options = new DbContextOptionsBuilder <MoviesDBContext>()
                          .UseInMemoryDatabase("no_changes_test").Options;

            using (var db = new MoviesDBContext(options))
            {
                // do nothing
                // (just creating the DbContext class with in-memory options
                //  automatically creates the in-memory DB too.)
            }

            // act
            using (var db = new MoviesDBContext(options))
            {
                var repo = new MovieRepository(db);
                repo.SaveChanges(); // assert
            }

            // assert
            // (no exception should have been thrown)
        }
예제 #14
0
 public MovieController(MoviesDBContext db)
 {
     this.db = db;
 }
예제 #15
0
 public AdminService(MoviesDBContext context, ILoggerManager loggerManager)
 {
     _context       = context;
     _loggerManager = loggerManager;
 }
 public HomeController(ILogger <HomeController> logger, IMoviesRepository repository, MoviesDBContext context)
 {
     _logger     = logger;
     _repository = repository;
     _context    = context;
 }
예제 #17
0
 public genreController(MoviesDBContext context)
 {
     _context = context;
 }
예제 #18
0
        public static int staticID; //global variable to store id of editing movie between pages

        //Constructor
        public HomeController(ILogger <HomeController> logger, MoviesDBContext context)
        {
            _logger  = logger;
            _context = context;
        }
예제 #19
0
 public EfRepository(MoviesDBContext dbContext)
 {
     _dbContext = dbContext;
 }
예제 #20
0
 public MoviesController()
 {
     MoviesDBContext.Intialize();
 }
예제 #21
0
 public RentalRepository(MoviesDBContext context)
 {
     _context = context;
 }
예제 #22
0
 public MoviesComingSoonDBModelsController(MoviesDBContext context)
 {
     _context = context;
 }
예제 #23
0
 public ProfileController(MoviesDBContext context)
 {
     _context = context;
 }
예제 #24
0
 public MovieService(MoviesDBContext context)
 {
     _context = context;
 }
 public TopRatedIndian2ModelController(MoviesDBContext context)
 {
     _context = context;
 }
예제 #26
0
 public MoviesController(MoviesDBContext context)
 {
     _context = context;
 }
예제 #27
0
 public HomeController(MoviesDBContext con)
 {
     context = con;
 }
예제 #28
0
 public UserRepository(MoviesDBContext context)
 {
     _context = context;
 }
 public MoviesInTheatersModelsController(MoviesDBContext context)
 {
     _context = context;
 }
예제 #30
0
 public TopRatedMovies1ModelController(MoviesDBContext context)
 {
     _context = context;
 }
예제 #31
0
 public HomeController(MoviesDBContext dBContextMovies, IMovieService movieService)
 {
     _DBContextMovies = dBContextMovies;
     _MovieService    = movieService;
 }