예제 #1
0
파일: Delete.cs 프로젝트: mishapost/Course
        public static void DeleteCharacter()
        {
            var dbContext = new CharactersDbContext();
            var character = dbContext.Characters.FirstOrDefault(x => x.FirstName == "John");

            dbContext.Characters.Remove(character);

            dbContext.SaveChanges();
            Console.WriteLine("Character deleted");
        }
예제 #2
0
파일: AddData.cs 프로젝트: mishapost/Course
        public static void AddCharactersToDb()
        {
            var dbContext  = new CharactersDbContext();
            var characters = CharactersRepository.GetCharacters();

            foreach (var character in characters)
            {
                dbContext.Characters.Add(character);
            }

            dbContext.SaveChanges();
            Console.WriteLine("Characters added");
        }
예제 #3
0
        public static void UpdateCharacter()
        {
            var dbContext = new CharactersDbContext();
            var character = dbContext.Characters.First();

            character.FirstName = "Tom";
            character.LastName  = "Riddle";
            character.Gender    = true;
            character.Age       = 17;

            dbContext.SaveChanges();
            Console.WriteLine("Character updated");
        }
예제 #4
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new CharactersDbContext(
                       serviceProvider.GetRequiredService <DbContextOptions <CharactersDbContext> >()))
            {
                if (context.Characters.Any())
                {
                    return;
                }

                context.Characters.AddRange(
                    new CharacterDBO(1, "Luke Skywalker"),
                    new CharacterDBO(2, "Dark Vader"),
                    new CharacterDBO(3, "Han Solo"),
                    new CharacterDBO(4, "Leia Organa", "Alderaan"),
                    new CharacterDBO(5, "Wilhuff Tarkin"),
                    new CharacterDBO(6, "C-3PO"),
                    new CharacterDBO(7, "R2-D2")
                    );

                context.Friends.AddRange(
                    new CharacterFriendDBO(1, 3),
                    new CharacterFriendDBO(1, 4),
                    new CharacterFriendDBO(1, 6),
                    new CharacterFriendDBO(1, 7),
                    new CharacterFriendDBO(2, 5),
                    new CharacterFriendDBO(3, 4),
                    new CharacterFriendDBO(3, 7),
                    new CharacterFriendDBO(4, 6),
                    new CharacterFriendDBO(4, 7),
                    new CharacterFriendDBO(6, 7)
                    );
                context.SaveChanges();
            }

            using (var episodeContext = new EpisodesDbContext(
                       serviceProvider.GetRequiredService <DbContextOptions <EpisodesDbContext> >()))
            {
                if (episodeContext.Episodes.Any())
                {
                    return;
                }

                episodeContext.Episodes.AddRange(
                    new EpisodeDBO(4, "NEWHOPE"),
                    new EpisodeDBO(5, "EMPIRE"),
                    new EpisodeDBO(6, "JEDI")
                    );

                episodeContext.EpisodeCharacters.AddRange(
                    new EpisodeCharacterDBO(4, 1),
                    new EpisodeCharacterDBO(5, 1),
                    new EpisodeCharacterDBO(6, 1),
                    new EpisodeCharacterDBO(4, 2),
                    new EpisodeCharacterDBO(5, 2),
                    new EpisodeCharacterDBO(6, 2),
                    new EpisodeCharacterDBO(4, 3),
                    new EpisodeCharacterDBO(5, 3),
                    new EpisodeCharacterDBO(6, 3),
                    new EpisodeCharacterDBO(4, 4),
                    new EpisodeCharacterDBO(5, 4),
                    new EpisodeCharacterDBO(6, 4),
                    new EpisodeCharacterDBO(4, 6),
                    new EpisodeCharacterDBO(5, 6),
                    new EpisodeCharacterDBO(6, 6),
                    new EpisodeCharacterDBO(4, 7),
                    new EpisodeCharacterDBO(5, 7),
                    new EpisodeCharacterDBO(6, 7),
                    new EpisodeCharacterDBO(5, 5)
                    );

                episodeContext.SaveChanges();
            }
        }