public static int EnsureSeedData(this DwContext context)
        {
            var bookCount       = default(int);
            var characterCount  = default(int);
            var bookSeriesCount = default(int);

            // Because each of the following seed method needs to do a save
            // (the data they're importing is relational), we need to call
            // SaveAsync within each method.
            // So let's keep tabs on the counts as they come back

            var dbSeeder = new DatabaseSeeder(context);

            if (!context.Books.Any())
            {
                var pathToSeedData = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "BookSeedData.json");
                bookCount = dbSeeder.SeedBookEntitiesFromJson(pathToSeedData).Result;
            }
            if (!context.BookCharacters.Any())
            {
                characterCount = dbSeeder.SeedBookCharacterEntriesFromJson().Result;
            }
            if (!context.BookSeries.Any())
            {
                bookSeriesCount = dbSeeder.SeedBookSeriesEntriesFromJson().Result;
            }

            return(bookCount + characterCount + bookSeriesCount);
        }
        public int SeedDatabase()
        {
            var entitiesAdded = default(int);
            var seeder        = new DatabaseSeeder(_context);

            if (!_context.Books.Any())
            {
                var pathToBookSeedData = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "BookSeedData.json");;
                entitiesAdded += seeder.SeedBookEntitiesFromJson(pathToBookSeedData);
            }

            if (!_context.BookCharacters.Any())
            {
                entitiesAdded += seeder.SeedBookCharacterEntriesFromJson();
            }

            if (!_context.BookSeries.Any())
            {
                entitiesAdded += seeder.SeedBookSeriesEntriesFromJson();
            }



            return(entitiesAdded);
        }
Beispiel #3
0
        public static void EnsureSeedData(this DwContext context)
        {
            if (context.AllMigrationsApplied())
            {
                var dbSeeder = new DatabaseSeeder(context);
                if (!context.Books.Any())
                {
                    var pathToSeedData = Path.Combine(Directory.GetCurrentDirectory(), "SeedData", "BookSeedData.json");;
                    dbSeeder.SeedBookEntitiesFromJson(pathToSeedData);
                }
                if (!context.BookCharacters.Any())
                {
                    dbSeeder.SeedBookCharacterEntriesFromJson();
                }
                if (!context.BookSeries.Any())
                {
                    dbSeeder.SeedBookSeriesEntriesFromJson();
                }

                context.SaveChanges();
            }
        }