Esempio n. 1
0
        public static void AddTagsToVideo(int videoId, params string[] tagNames)
        {
            using (var context = new VidAppContext())
            {
                // Mit LINQ:
                // SELECT FROM Tags WHERE Name IN ('classics', 'drama')
                var tags = context.Tags.Where(t => tagNames.Contains(t.Name)).ToList();

                foreach (var tagName in tagNames)
                {
                    if (!tags.Any(t => t.Name.Equals(tagName, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        tags.Add(new Tag {
                            Name = tagName
                        });
                    }
                }

                var video = context.Videos.Single(v => v.Id == videoId);

                tags.ForEach(t => video.AddTag(t));

                context.SaveChanges();
            }
        }
Esempio n. 2
0
 public static void AddVideo(Video video)
 {
     using (var context = new VidAppContext())
     {
         context.Videos.Add(video);
         context.SaveChanges();
     }
 }
Esempio n. 3
0
        static void Main(string[] args)
        {
            using (var context = new VidAppContext()) {
                context.Database.Log = Console.WriteLine;

                context.AddVideo("Video 4", DateTime.Today, "Horror", (byte)Classification.Gold);
            }
        }
Esempio n. 4
0
        public static void RemoveVideo(int videoId)
        {
            using (var context = new VidAppContext())
            {
                var video = context.Videos.SingleOrDefault(v => v.Id == videoId);
                if (video == null)
                {
                    return;
                }

                context.Videos.Remove(video);
                context.SaveChanges();
            }
        }
Esempio n. 5
0
        public static void RemoveTagsFromVideo(int videoId, params string[] tagNames)
        {
            using (var context = new VidAppContext())
            {
                context.Tags.Where(t => tagNames.Contains(t.Name)).Load();

                var video = context.Videos.Single(v => v.Id == videoId);

                foreach (var tagName in tagNames)
                {
                    // Wir haben die Logik für das Entfernen von Tags in der Video-Klasse gekapselt.
                    // Das ist sauberes OO. Die Video-Klasse soll verantwortlich sein, um Tags
                    // um Tags zu ihrer Liste hinzuzufügen bzw. zu entfernen.
                    video.RemoveTag(tagName);
                }

                context.SaveChanges();
            }
        }
Esempio n. 6
0
        public static void RemoveGenre(int genreId, bool enforceDeletingVideos)
        {
            using (var context = new VidAppContext())
            {
                var genre = context.Genres.Include(g => g.Videos).SingleOrDefault(g => g.Id == genreId);
                if (genre == null)
                {
                    return;
                }

                if (enforceDeletingVideos)
                {
                    context.Videos.RemoveRange(genre.Videos);
                }

                context.Genres.Remove(genre);
                context.SaveChanges();
            }
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            // Um LazyLoading zu aktivieren, müssen die Navigation-Properties als virtual definiert werden,
            // damit EF ein Proxy erstellen kann.

            using (var context = new VidAppContext()) {
                context.Database.Log = Console.WriteLine;

                var videos = context.Videos.ToList();

                Console.WriteLine();
                Console.WriteLine("LAZY LOADING");
                foreach (var v in videos)
                {
                    Console.WriteLine("{0} ({1})", v.Name, v.Genre.Name);
                }

                // Eager loading
                var videosWithGenres = context.Videos.Include(v => v.Genre).ToList();

                Console.WriteLine();
                Console.WriteLine("EAGER LOADING");
                foreach (var v in videosWithGenres)
                {
                    Console.WriteLine("{0} ({1})", v.Name, v.Genre.Name);
                }

                // Explicit loading

                // NOTE: At this point, genres are already loaded into the context,
                // so the following line is not going to make a difference. If you
                // want to see expicit loading in action, comment out the eager loading
                // part as well as the foreach block in the lazy loading.
                context.Genres.Load();

                Console.WriteLine();
                Console.WriteLine("EXPLICIT LOADING");
                foreach (var v in videos)
                {
                    Console.WriteLine("{0} ({1})", v.Name, v.Genre.Name);
                }
            }
        }
Esempio n. 8
0
        public static void AddTags(params string[] tagNames)
        {
            using (var context = new VidAppContext())
            {
                // Wir laden die Tags vorgängig um Duplikate zu vermeiten
                var tags = context.Tags.Where(t => tagNames.Contains(t.Name)).ToList();

                foreach (var name in tagNames)
                {
                    if (!tags.Any(t => t.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        context.Tags.Add(new Tag {
                            Name = name
                        });
                    }
                }

                context.SaveChanges();
            }
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            using (var context = new VidAppContext()) {
                context.Database.Log = Console.WriteLine;

                // Aufgabe 1: Actionfilme sortiert nach Name
                Console.WriteLine();
                Console.WriteLine("ACTION MOVIES SORTED BY NAME");
                var actionMovies = context.Videos
                                   .Where(v => v.Genre.Name == "Action")
                                   .OrderBy(v => v.Name);

                foreach (var v in actionMovies)
                {
                    Console.WriteLine(v.Name);
                }

                // Aufgabe 2: Gold Drama File sortiert nach Erscheinungsdatum (ReleaseDate). Neuster Film zuerst.
                var dramaMovies = context.Videos
                                  .Where(v => v.Genre.Name == "Drama" && v.Classification == Classification.Gold)
                                  .OrderByDescending(v => v.ReleaseDate);

                Console.WriteLine();
                Console.WriteLine("GOLD DRAMA MOVIES SORTED BY RELEASE DATE (NEWEST FIRST)");
                foreach (var v in dramaMovies)
                {
                    Console.WriteLine(v.Name);
                }

                // Aufgabe 3: Alle Filme projiziert in einen anonymen Typ
                var projected = context.Videos
                                .Select(v => new { MovieName = v.Name, Genre = v.Genre.Name });

                Console.WriteLine();
                Console.WriteLine("ALL MOVIES PROJECTED INTO AN ANONYMOUS TYPE");
                foreach (var v in projected)
                {
                    Console.WriteLine(v.MovieName);
                }

                // Aufgabe 4: Alle Filme gruppiert nach Classification
                var groups = context.Videos
                             .GroupBy(v => v.Classification)
                             .Select(g => new {
                    Classification = g.Key.ToString(),
                    Videos         = g.OrderBy(v => v.Name)
                });

                Console.WriteLine();
                Console.WriteLine("ALL MOVIES GROUPED BY CLASSIFICATION");
                foreach (var g in groups)
                {
                    Console.WriteLine("Classification: " + g.Classification);

                    foreach (var v in g.Videos)
                    {
                        Console.WriteLine("\t" + v.Name);
                    }
                }

                // Aufgabe 5: Classifications and Anzahl Videos
                var classifications = context.Videos
                                      .GroupBy(v => v.Classification)
                                      .Select(g => new {
                    Name        = g.Key.ToString(),
                    VideosCount = g.Count()
                })
                                      .OrderBy(c => c.Name);

                Console.WriteLine();
                Console.WriteLine("CLASSIFICATIONS AND NUMBER OF VIDEOS IN THEM");
                foreach (var c in classifications)
                {
                    Console.WriteLine("{0} ({1})", c.Name, c.VideosCount);
                }


                // Aufgabe 6: Genres and Anzahl Videos
                var genres = context.Genres
                             .GroupJoin(context.Videos, g => g.Id, v => v.GenreId, (genre, videos) => new {
                    Name        = genre.Name,
                    VideosCount = videos.Count()
                })
                             .OrderByDescending(g => g.VideosCount);

                Console.WriteLine();
                Console.WriteLine("GENRES AND NUMBER OF VIDEOS IN THEM");
                foreach (var g in genres)
                {
                    Console.WriteLine("{0} ({1})", g.Name, g.VideosCount);
                }
            }
        }