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);
        }
Exemple #3
0
        public void DbSeeder_SeedBookData_NoDataSupplied_ShouldThrowException()
        {
            // Arrange
            var bookList    = new List <Book>();
            var mockBookSet = DbSetHelpers.GetQueryableDbSet(bookList);
            var mockset     = new Mock <IDwContext>();

            mockset.Setup(m => m.Books).Returns(mockBookSet.Object);

            // Act & Assert
            var dbSeeder            = new DatabaseSeeder(mockset.Object);
            ArgumentException argEx = Assert.Throws <ArgumentException>(() =>
                                                                        dbSeeder.SeedBookEntitiesFromJson(string.Empty));
        }
Exemple #4
0
        public void DbSeeder_SeedBookData_DataSupplied_ShouldNotThrowException()
        {
            // Arrange
            // TODO Add an interface here, to mock stuff properly
            var bookList    = new List <Book>();
            var mockBookSet = DbSetHelpers.GetQueryableDbSet(bookList);
            var mockset     = new Mock <IDwContext>();

            mockset.Setup(m => m.Books).Returns(mockBookSet.Object);
            var testJsonDirectory = Path.Combine(Directory.GetCurrentDirectory(), "SeedData");
            var pathToSeedData    = Path.Combine(testJsonDirectory, "TestBookSeedData.json");

            // Act & Assert
            var dbSeeder = new DatabaseSeeder(mockset.Object);

            dbSeeder.SeedBookEntitiesFromJson(pathToSeedData);
        }
        public static void EnsureSeedData(this DwContext context)
        {
            if (context.AllMigrationsApplied())
            {
                var dbSeeder = new DatabaseSeeder(context);
                if (!context.Books.Any())
                {
                    dbSeeder.SeedBookEntitiesFromJson();
                }
                if (!context.Characters.Any())
                {
                    dbSeeder.SeedCharacterEntitiesFromJson();
                }
                if (!context.BookCharacters.Any())
                {
                    dbSeeder.SeedBookCharacterEntriesFromJson();
                }

                context.SaveChanges();
            }
        }
        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();
            }
        }