예제 #1
0
 public EFBookAppRepository(BookAppDbContext context)
 {
     _context = context;
 }
예제 #2
0
        public static void EnsurePopulated(IApplicationBuilder app)
        {
            // set up the context
            BookAppDbContext context = app.ApplicationServices.CreateScope()
                                       .ServiceProvider.GetRequiredService <BookAppDbContext>();

            if (context.Database.GetPendingMigrations().Any())
            {
                context.Database.Migrate();
            }

            // add books to database if none exist
            if (!context.Books.Any())
            {
                context.AddRange(
                    new Book
                {
                    Title           = "Les Miserables",
                    AuthorFirstName = "Victor",
                    AuthorLastName  = "Hugo",
                    Publisher       = "Signet",
                    ISBN            = "978-0451419439",
                    Classification  = "Fiction",
                    Category        = "Classic",
                    Price           = 9.95M
                },
                    new Book
                {
                    Title               = "Team of Rivals",
                    AuthorFirstName     = "Doris",
                    AuthorMiddleInitial = "K",
                    AuthorLastName      = "Goodwin",
                    Publisher           = "Simon & Schuster",
                    ISBN           = "978-0743270755",
                    Classification = "Non-Fiction",
                    Category       = "Biography",
                    Price          = 14.58M
                },
                    new Book
                {
                    Title           = "The Snowball",
                    AuthorFirstName = "Alice",
                    AuthorLastName  = "Schroeder",
                    Publisher       = "Bantam",
                    ISBN            = "978-0553384611",
                    Classification  = "Non-Fiction",
                    Category        = "Biography",
                    Price           = 21.54M
                },
                    new Book
                {
                    Title               = "American Ulysses",
                    AuthorFirstName     = "Ronald",
                    AuthorMiddleInitial = "C",
                    AuthorLastName      = "White",
                    Publisher           = "Random House",
                    ISBN           = "978-0812981254",
                    Classification = "Non-Fiction",
                    Category       = "Biography",
                    Price          = 11.61M
                }
                    );

                context.SaveChanges();
            }
        }