public static int SeedDatabase(this SqlDbContext context, string wwwrootDirectory)
        {
            var numBooks = context.Books.Count();

            if (numBooks == 0)
            {
                //the database is emply so we fill it from a json file
                var books = BookJsonLoader.LoadBooks(Path.Combine(wwwrootDirectory, SeedFileSubDirectory),
                                                     SeedDataSearchName).ToList();
                context.Books.AddRange(books);
                context.SaveChanges();
                //We add this separately so that it has the highest Id. That will make it appear at the top of the default list
                context.Books.Add(SpecialBook.CreateSpecialBook());
                context.SaveChanges();
                numBooks = books.Count + 1;
            }

            return(numBooks);
        }
Esempio n. 2
0
        public static int SeedDatabase(this SqlDbContext context, string wwwrootDirectory)
        {
            var numBooks = context.Books.Count();

            if (numBooks == 0)
            {
                //the database is empty so we fill it from a json file
                //This also sets up the NoSql database IF the IBookUpdater member is registered.
                var books = BookJsonLoader.LoadBooks(Path.Combine(wwwrootDirectory, SeedFileSubDirectory),
                                                     SeedDataSearchName).ToList();
                context.Books.AddRange(books);
                context.SaveChanges();

                //we add the special book
                context.Books.Add(SpecialBook.CreateSpecialBook());
                context.SaveChanges();
                numBooks = books.Count + 1;
            }

            return(numBooks);
        }
Esempio n. 3
0
        public static int SeedDatabase(this EfCoreContext context, string dataDirectory)
        {
            if (!(context.GetService <IDatabaseCreator>() as RelationalDatabaseCreator).Exists())
            {
                throw new InvalidOperationException("The database does not exist. If you are using Migrations then run PMC command update-database to create it");
            }

            var numBooks = context.Books.Count();

            if (numBooks == 0)
            {
                //the database is emply so we fill it from a json file
                var books = BookJsonLoader.LoadBooks(Path.Combine(dataDirectory, SeedFileSubDirectory),
                                                     SeedDataSearchName).ToList();
                context.Books.AddRange(books);
                context.SaveChanges();
                //We add this separately so that it has the highest Id. That will make it appear at the top of the default list
                context.Books.Add(SpecialBook.CreateSpecialBook());
                context.SaveChanges();
                numBooks = books.Count + 1;
            }

            return(numBooks);
        }