Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // Get a new context which will connect to the database
            var context = new SuncoastMoviesContext();

            // Get a reference to our collection of movies.
            // NOTE: this doesn't yet access any of them, just gives
            //       us a variable that knows how.

            // var movies = context.Movies;


            var movieCount = context.Movies.Count();

            Console.WriteLine($"There are {movieCount} movies!");

            foreach (var movie in context.Movies)
            {
                Console.WriteLine($"There is a movie name {movie.Title} ");
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            // Now lets go through the movies *AND* join their Ratings
            //
            //
            //
            //
            //                   GET ALL THE MOVIES
            //                      |        |
            foreach (var movie in context.Movies.Include(movie => movie.Rating))
            {
                Console.WriteLine($"There is a movie named {movie.Title} that is the {movie.Rating.Description} ");
            }
            foreach (var movie in movies)
            {
                if (movie.Rating == null)
                {
                    Console.WriteLine($"{movie.Title} - not rated");
                }

                else
                {
                    Console.WriteLine($"{movie.Title} - {movie.Rating.Description}");
                }

                foreach (var role in movie.Roles)
                {
                    Console.WriteLine($" - {role.CharacterName} played by {role.Actor.FullName}");
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // Get a new context which will connect to the database
            var context = new SuncoastMoviesContext();

            // Get a reference to our collection of movies.
            // NOTE: this doesn't yet access any of them, just gives
            //       us a variable that knows how.

            // Hey, context, give me all the Movies from the movies table
            var movies = context.Movies.
                         // ... but also join to the ratings table so for every
                         //     movie object I can ask for the Rating object
                         Include(movie => movie.Rating).
                         // ... but also join to the roles table so for every
                         //     movie object I can ask for the LIST of Role objects.
                         Include(movie => movie.Roles).
                         // .. but for each of those roles also join to the Actors
                         //    table so for each role object I can ask for the Actor object.
                         ThenInclude(role => role.Actor);


            var movieCount = movies.Count();

            Console.WriteLine($"There are {movieCount} movies!");

            foreach (var movie in movies)
            {
                if (movie.Rating == null)
                {
                    Console.WriteLine($"There is a movie named {movie.Title} and has not been rated yet");
                }
                else
                {
                    Console.WriteLine($"There is a movie named {movie.Title} and a rating of {movie.Rating.Description}");
                }

                foreach (var role in movie.Roles)
                {
                    var whatActorPlayedThisRole = role.Actor;

                    Console.WriteLine($" - Has a character named {role.CharacterName} played by {whatActorPlayedThisRole.FullName}");
                }
            }

            // var newMovie = new Movie
            // {
            //     Title = "SpaceBalls",
            //     PrimaryDirector = "Mel Brooks",
            //     Genre = "Comedy",
            //     YearReleased = 1987,
            //     RatingId = 2
            // };

            // // Add the newMovie to the context's Movies
            // context.Movies.Add(newMovie);

            // // Tell the context to save any changes.
            // context.SaveChanges();

            // Search for a movie by name. FirstOrDefault takes a function to use to compare the movies and returns the first record that matches, or if nothing matches, returns null.
            // This is the same as we used with LINQ against a List, but this time it is searching the database.
            var existingMovie = context.Movies.FirstOrDefault(movie => movie.Title == "SpaceBalls");

            // If we found an existing movie.
            if (existingMovie != null)
            {
                // Change the title of this movie.
                existingMovie.Title = "SpaceBalls - the best movie ever";

                // Ask the context to save changes.
                context.SaveChanges();
            }

            var existingMovieToDelete = context.Movies.FirstOrDefault(movie => movie.Title == "Cujo");

            // If we found an existing movie.
            if (existingMovieToDelete != null)
            {
                // Remove the existing movie from the collection
                context.Movies.Remove(existingMovieToDelete);

                // Ask the context to save changes, in this case deleting the record.
                context.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            // Get a new context that will connect to the database
            var context = new SuncoastMoviesContext();

            // Not too different from code we've seen before
            //
            // var transactionCount = transactions.Count();
            var moviesCount = context.Movies.Count();

            Console.WriteLine($"There are {moviesCount} movies in our database");

            // Makes a new collection of movies but each movie knows the associated Rating object
            var moviesWithRatingsRolesAndActors = context.Movies.
                                                  // from our movie, please include the associated Rating object
                                                  Include(movie => movie.Rating).
                                                  // ... and from our movie, please include the associated Roles LIST
                                                  Include(movie => movie.Roles).
                                                  // THEN for each of roles, please include the associated Actor object
                                                  ThenInclude(role => role.Actor);

            foreach (var movie in moviesWithRatingsRolesAndActors)
            {
                if (movie.Rating == null)
                {
                    Console.WriteLine($"There is a movie named {movie.Title}");
                }
                else
                {
                    Console.WriteLine($"Movie {movie.Title} - {movie.Rating.Description}");
                }

                foreach (var role in movie.Roles)
                {
                    Console.WriteLine($" - {role.CharacterName} is played by {role.Actor.FullName}");
                }
            }


            var newMovie = new Movie
            {
                Title           = "SpaceBalls",
                PrimaryDirector = "Mel Brooks",
                Genre           = "Comedy",
                YearReleased    = 1987,
                RatingId        = 2
            };

            // dinos.Add(newDino);
            // transactions.Add(newTransaction);
            context.Movies.Add(newMovie);
            context.SaveChanges();

            // Search for a movie by name. FirstOrDefault takes a function to use to compare the movies and returns the first record that matches, or if nothing matches, returns null.
            // This is the same as we used with LINQ against a List, but this time it is searching the database.
            var existingMovie = context.Movies.FirstOrDefault(movie => movie.Title == "SpaceBalls");

            // If we found an existing movie.
            if (existingMovie != null)
            {
                // Change the title of this movie.
                existingMovie.Title = "SpaceBalls - the best movie ever";

                // Ask the context to save changes.
                context.SaveChanges();
            }

            var existingMovieToDelete = context.Movies.FirstOrDefault(movie => movie.Title == "Cujo");

            // If we found an existing movie.
            if (existingMovieToDelete != null)
            {
                // Remove the existing movie from the collection
                context.Movies.Remove(existingMovieToDelete);

                // Ask the context to save changes, in this case deleting the record.
                context.SaveChanges();
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var context    = new SuncoastMoviesContext();
            var movies     = context.Movies;
            var movieCount = movies.Count(); // Using Linq on DbSet retrieved from database

            Console.WriteLine($"There are {movieCount} movies!");
            var moviesWithRatings = context.Movies                   // makes a new collection of movies but each movie knows the associated Rating object
                                    .Include(movie => movie.Rating). // from our movie, please include the associated rating
                                    Include(movie => movie.Roles).   // from our movie, please include the associated roles list
                                    ThenInclude(role => role.Actor); // THEN for each of the roles, please include associated actor object...join stmts in C# with includes, now we have access to all the data tables

            foreach (var movie in movies)
            {
                if (movie.Rating == null)
                {
                    Console.WriteLine($"There is an unrated movie named {movie.Title}");
                }
                else
                {
                    Console.WriteLine($"Movie {movie.Title} - movie.Rating.Description");
                }
            }
            foreach (var movie in movies)
            {
                if (movie.Rating == null)
                {
                    Console.WriteLine($"{movie.Title} - not rated");
                }
                else
                {
                    Console.WriteLine($"{movie.Title} - {movie.Rating.Description}");
                }
                foreach (var role in movie.Roles)
                {
                    Console.WriteLine($" - {role.CharacterName} played by {role.Actor.FullName}");
                }
            }
            var newMovie = new Movie
            {
                Title           = "SpaceBalls",
                PrimaryDirector = "Mel Brooks",
                Genre           = "Comedy",
                YearReleased    = 1987,
                RatingId        = 2
            };

            context.Movies.Add(newMovie); // DbSet can be treated like a list

            context.SaveChanges();        // Saves to database, must do this because it's hosted on a different computer, but add to the context (if not done, it will seriously slow down the program because it would have to connect with the database each time)
            // SaveChanges imparts atomicity
            var existingMovie = context.Movies.FirstOrDefault(movie => movie.Title == "SpaceBalls");

            if (existingMovie != null)
            {
                existingMovie.Title = "SpaceBalls - the best movie ever";
                context.SaveChanges();
            }
            var existingMovie2 = context.Movies.FirstOrDefault(movie => movie.Title == "Cujo");

            if (existingMovie2 != null)
            {
                context.Movies.Remove(existingMovie2);
                context.SaveChanges();
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            // Get a new context which will connect to the database
            var context = new SuncoastMoviesContext();

            // Get a reference to our collection of movies.
            // NOTE: this doesn't yet access any of them, just gives
            //       us a variable that knows how.
            // var movies = context.Movies;

            var movieCount = context.Movies.Count();

            Console.WriteLine($"There are {movieCount} movies!");

            foreach (var movie in context.Movies)
            {
                Console.WriteLine($"There is a movie named {movie.Title}");
            }
            Console.WriteLine("\n\n\n");

            // Now lets go through the movies *AND* join their Ratings
            //
            //                    GET ALL THE MOVIES
            //                    |            |
            //                    |            |   Include is like JOIN
            //                    |            |     |
            //                    |            |     |            What to join with
            //                    |            |     |            |
            //                    v            v     v            v
            foreach (var movie in context.Movies.Include(movie => movie.TheRatingAssociatedToTheMovieObject))
            {
                Console.WriteLine($"There is a movie named {movie.Title} that is rated {movie.TheRatingAssociatedToTheMovieObject.Description}");
            }

            Console.WriteLine("\n\n\n");
            //                                   JOIN TO ROLES                 SECOND JOIN FROM ROLES TO ACTOR
            foreach (var movie in context.Movies.Include(movie => movie.Roles).ThenInclude(role => role.TheAssociatedActor))
            {
                Console.WriteLine($"The movie {movie.Title}");

                foreach (var role in movie.Roles)
                {
                    Console.WriteLine($"- {role.CharacterName} played by {role.TheAssociatedActor.FullName}");
                }
            }

            // Find the rating from the database who's Description is "PG-13"
            //
            // So that we can assign the correct RatingId for the movie we are about to create
            var rating = context.Ratings.First(rating => rating.Description == "PG-13");

            // Make a new movie
            var newMovie = new Movie
            {
                Title           = "SpaceBalls",
                PrimaryDirector = "Mel Brooks",
                Genre           = "Comedy",
                YearReleased    = 1987,
                // Use the rating object's `Id` to assing this movie's RatingId
                RatingId = rating.Id,
            };

            // // Queues up the addition of this movie to the movies table
            // context.Movies.Add(newMovie);

            // // DO IT! ACTUALLY ADD THE MOVIE
            // context.SaveChanges();


            var existingMovie = context.Movies.FirstOrDefault(movie => movie.Title == "SpaceBalls");

            if (existingMovie == null)
            {
                Console.WriteLine("Couldn't find the movie. Uh oh!");
            }
            else
            {
                Console.WriteLine("Updating the movie!");

                // Change the name of the movie
                existingMovie.Title = "SpaceBalls - best movie ever";

                // DO IT! (make the change in the database)
                context.SaveChanges();
            }


            // Find the first movie who's title is Cujo, if not found, existingMovieToDelete will be `null`
            var existingMovieToDelete = context.Movies.FirstOrDefault(movie => movie.Title == "Cujo");

            // If we found a movie that has the Title Cujo, then...
            if (existingMovieToDelete != null)
            {
                context.Movies.Remove(existingMovieToDelete);

                context.SaveChanges();
            }
        }