Beispiel #1
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                throw new ArgumentException("Please specify the connection strings!");
            }

            var masterConnectionString = args[0];
            var dbConnectionString     = args[1];

            DatabaseFeeder.RunAllScripts(masterConnectionString, dbConnectionString, @"..\..\data\");
            Console.ReadLine();
        }
        public static void RunAllScripts(string masterConnectionString, string dbConnectionString, string folder)
        {
            var books = GetDtoData <BookDto>(folder + "books.json").Select(b => b.ToBookEntity()).ToList();

            Console.WriteLine($"Parsed {books.Count} number of books");

            var characters = GetDtoData <CharacterDto>(folder + "characters.json").Select(c => c.ToCharacterEntity()).ToList();

            Console.WriteLine($"Parsed {characters.Count} number of characters");

            var houses = GetDtoData <HouseDto>(folder + "houses.json").Select(h => h.ToHouseEntity()).ToList();

            Console.WriteLine($"Parsed {houses.Count} number of houses");

#if DEBUG
            try
            {
                DatabaseFeeder.CleanDatabase(masterConnectionString);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
#endif
            try
            {
                DatabaseFeeder.CreateDatabase(masterConnectionString);
                Console.WriteLine("Created database");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create database: " + e.Message);
            }

            try
            {
                DatabaseFeeder.CreateTables(dbConnectionString);
                Console.WriteLine("Created tables");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create tables: " + e.Message);
            }


            var options = new OptionsWrapper <ConnectionOptions>(new ConnectionOptions()
            {
                ConnectionString = dbConnectionString
            });

            try
            {
                var bookRepo = new BookRepository(options);
                bookRepo.InsertEntitiesAsync(books).GetAwaiter().GetResult();
                Console.WriteLine("Finished inserting book data");

                var characterRepo = new CharacterRepository(options);
                characterRepo.InsertEntitiesAsync(characters).GetAwaiter().GetResult();
                Console.WriteLine("Finished inserting character data");

                var houseRepo = new HouseRepository(options);
                houseRepo.InsertEntitiesAsync(houses).GetAwaiter().GetResult();
                Console.WriteLine("Finished inserting house data");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }