//Creating a repository of book context public EFBookRepository(BookDBContext context) { _context = context; }
public static void EnsurePopulated(IApplicationBuilder application) { BookDBContext context = application.ApplicationServices. CreateScope().ServiceProvider.GetRequiredService <BookDBContext>(); //if we need to migrate, do so if (context.Database.GetPendingMigrations().Any()) { context.Database.Migrate(); } //if there are not any books, create them if (!context.Books.Any()) { context.Books.AddRange( new Book { //removed becasue the model has id set as KEY //BookId = 1, Title = "Les Miserables", Author = "Victor Hugo", Publisher = "Signet", ISBN = "978-0451419439", //Classification = "Fiction, Classic", Classification = "Classic", Pages = 1488, Price = 9.95 }, new Book { //BookId = 2, Title = "Team of Rivals", Author = "Doris Kearns Goodwin", Publisher = "Simon and Schuster", ISBN = "978-0743270755", //Classification = "Non-Fiction, Biography", Classification = "Biography", Pages = 944, Price = 14.95 }, new Book { //BookId = 3, Title = "The Snowball", Author = "Alice Schroeder", Publisher = "Bantam", ISBN = "978-0553384611", //Classification = "Non-Fiction, Biography", Classification = "Biography", Pages = 832, Price = 21.54 }, new Book { //BookId = 4, Title = "American Ulysses", Author = "Ronald C White", Publisher = "Random House", ISBN = "978-0812981254", //Classification = "Non-Fiction, Biography", Classification = "Biography", Pages = 864, Price = 11.61 }, new Book { //BookId = 5, Title = "Unbroken", Author = "Laura Hillenbrand", Publisher = "Random House", ISBN = "978-0812974492", //Classification = "Non-Fiction, Biography", Classification = "Biography", Pages = 528, Price = 13.33 }, new Book { //BookId = 6, Title = "The Great Train Robbery", Author = "Michael Crichton", Publisher = "Vintage", ISBN = "978-0804171281", //Classification = "Fiction, Historical Fiction", Classification = "Historical Fiction", Pages = 288, Price = 15.95 }, new Book { //BookId = 7, Title = "Deep Work", Author = "Cal Newport", Publisher = "Grand Central Publishing", ISBN = "978-1455586691", //Classification = "Non-Fiction, Self-Help", Classification = "Self-Help", Pages = 304, Price = 14.99 }, new Book { //BookId = 8, Title = "It's Your Ship", Author = "Michael Abrashoff", Publisher = "Grand Central Publishing", ISBN = "978-1455523023", //Classification = "Non-Fiction, Self-Help", Classification = "Self-Help", Pages = 240, Price = 21.66 }, new Book { //BookId = 9, Title = "The Virgin Way", Author = "Richard Branson", Publisher = "Portfolio", ISBN = "978-1591847984", //Classification = "Non-Fiction, Business", Classification = "Business", Pages = 400, Price = 29.16 }, new Book { //BookId = 10, Title = "Sycamore Row", Author = "Josh Grisham", Publisher = "Bantam", ISBN = "978-0553393613", //Classification = "Fiction, Thrillers", Classification = "Thrillers", Pages = 462, Price = 15.03 }, //the following are books that I added to the seed data new Book { //BookId = 11, Title = "Little Women", Author = "Lousia May Alcott", Publisher = "Signet Classics", ISBN = "978-0553212754", //Classification = "Fiction, Comedy", Classification = "Comedy", Pages = 507, Price = 5.89 }, new Book { //BookId = 12, Title = "Bury My Heart At Wounded Knee", Author = "Dee Brown", Publisher = "Picador", ISBN = "978-0805086843", //Classification = "Non-Fiction, History", Classification = "History", Pages = 512, Price = 13.99 }, new Book { //BookId = 13, Title = "Clash of Kings", Author = "George RR Martin", Publisher = "Bantam", ISBN = "978-0553579901", //Classification = "Fiction, Fantasy", Classification = "Fantasy", Pages = 1040, Price = 9.99 } ); //save changes to database context.SaveChanges(); } }
public static void EnsurePopulated(IApplicationBuilder application) { BookDBContext context = application.ApplicationServices.CreateScope().ServiceProvider.GetRequiredService <BookDBContext>(); if (context.Database.GetPendingMigrations().Any()) { context.Database.Migrate(); } if (!context.Books.Any()) { context.Books.AddRange( new Book { BookTitle = "Les Miserables", AuthorFirstName = "Victor", AuthorLastName = "Hugo", BookPublisher = "Signet", ISBN = "978-0451419439", Classification = "Fiction", Category = "Classic", NumPages = 1488, Price = 9.95F }, new Book { BookTitle = "Team of Rivals", AuthorFirstName = "Doris", AuthorMiddleName = "Kearns", AuthorLastName = "Goodwin", BookPublisher = "Simon & Schuster", ISBN = "978-0743270755", Classification = "Non-Fiction", Category = "Biography", NumPages = 944, Price = 14.58F }, new Book { BookTitle = "The Snowball", AuthorFirstName = "Alice", AuthorLastName = "Schroeder", BookPublisher = "Bantam", ISBN = "978-0553384611", Classification = "Non-Fiction", Category = "Biography", NumPages = 832, Price = 21.54F }, new Book { BookTitle = "American Ulysses", AuthorFirstName = "Ronald", AuthorMiddleName = "C.", AuthorLastName = "White", BookPublisher = "Random House", ISBN = "978-0812981254", Classification = "Non-Fiction", Category = "Biography", NumPages = 864, Price = 11.61F }, new Book { BookTitle = "Unbroken", AuthorFirstName = "Laura", AuthorLastName = "Hillenbrand", BookPublisher = "Random House", ISBN = "978-0812974492", Classification = "Non-Fiction", Category = "Historical", NumPages = 528, Price = 13.33F }, new Book { BookTitle = "The Great Train Robbery", AuthorFirstName = "Michael", AuthorLastName = "Crichton", BookPublisher = "Vintage", ISBN = "978-0804171281", Classification = "Fiction", Category = "Historical Fiction", NumPages = 288, Price = 15.95F }, new Book { BookTitle = "Deep Work", AuthorFirstName = "Cal", AuthorLastName = "Newport", BookPublisher = "Grand Central Publishing", ISBN = "978-1455586691", Classification = "Non-Fiction", Category = "Self-Help", NumPages = 304, Price = 14.99F }, new Book { BookTitle = "It's Your Ship", AuthorFirstName = "Michael", AuthorLastName = "Abrashoff", BookPublisher = "Grand Central Publishing", ISBN = "978-1455523023", Classification = "Non-Fiction", Category = "Self-Help", NumPages = 240, Price = 21.66F }, new Book { BookTitle = "The Virgin Way", AuthorFirstName = "Richard", AuthorLastName = "Branson", BookPublisher = "Portfolio", ISBN = "978-1591847984", Classification = "Non-Fiction", Category = "Business", NumPages = 400, Price = 29.16F }, new Book { BookTitle = "Sycamore Row", AuthorFirstName = "John", AuthorLastName = "Grisham", BookPublisher = "Bantam", ISBN = "978-0553393613", Classification = "Fiction", Category = "Thrillers", NumPages = 642, Price = 15.03F }, new Book { BookTitle = "Harry Potter and the Deathly Hallows", AuthorFirstName = "Joanne", AuthorMiddleName = "K", AuthorLastName = "Rowling", BookPublisher = "Scholastic", ISBN = "978-0545029377", Classification = "Fiction", Category = "Magic", NumPages = 759, Price = 8.47F }, new Book { BookTitle = "The Return of the King", AuthorFirstName = "John", AuthorMiddleName = "Ronald Reuel", AuthorLastName = "Tolkien", BookPublisher = "George Allen & Unwin", ISBN = "978-0358380252", Classification = "Fiction", Category = "Thrillers", NumPages = 416, Price = 11.19F }, new Book { BookTitle = "Crime and Punishment", AuthorFirstName = "Fyodor", AuthorLastName = "Dostoevsky", BookPublisher = "Vintage", ISBN = "978-0679734505", Classification = "Fiction", Category = "Crime", NumPages = 531, Price = 15.95F } ); context.SaveChanges(); } }
//This class automatically adds data to the database public static void EnsurePopulated(IApplicationBuilder application) { BookDBContext context = application.ApplicationServices.CreateScope().ServiceProvider.GetRequiredService <BookDBContext>(); if (context.Database.GetPendingMigrations().Any()) { context.Database.Migrate(); } //These are all the current books in the database. The BookID field is automatically generated //I'm really good at redoing migrations now if (!context.Books.Any()) { context.Books.AddRange( new Book { Title = "Les Miserables", AuthorFirst = "Victor", AuthorLast = "Hugo", AuthorMiddle = "", Publisher = "Signet", ISBN = "978-0451419439", Class = "Fiction", Cat = "Classic", Price = 9.95, PageNum = 1488 }, new Book { Title = "Team Rivals", AuthorFirst = "Doris", AuthorLast = "Goodwin", AuthorMiddle = "Kearns", Publisher = "Simon & Schuster", ISBN = "978-0743270755", Class = "Non-fiction", Cat = "Biography", Price = 14.58, PageNum = 944 }, new Book { Title = "The Snowball", AuthorFirst = "Alice", AuthorLast = "Shroeder", AuthorMiddle = "", Publisher = "Bantam", ISBN = "978-0553384611", Class = "Non-fiction", Cat = "Biography", Price = 21.54, PageNum = 832 }, new Book { Title = "American Ulysses", AuthorFirst = "Ronald", AuthorLast = "White", AuthorMiddle = "C", Publisher = "Random House", ISBN = "978-0812981254", Class = "Non-fiction", Cat = "Biography", Price = 11.61, PageNum = 864 }, new Book { Title = "Unbroken", AuthorFirst = "Lauren", AuthorLast = "Hildenbrand", AuthorMiddle = "", Publisher = "Random House", ISBN = "978-0812974492", Class = "Non-fiction", Cat = "Historical", Price = 13.33, PageNum = 528 }, new Book { Title = "The Great Train Robbery", AuthorFirst = "Michael", AuthorLast = "Crichton", AuthorMiddle = "", Publisher = "Vintage", ISBN = "978-0804171281", Class = "Fiction", Cat = "Historical Fiction", Price = 15.95, PageNum = 288 }, new Book { Title = "Deep Work", AuthorFirst = "Cal", AuthorLast = "Newport", AuthorMiddle = "", Publisher = "Grand Central Publishing", ISBN = "978-1455523023", Class = "Non-fiction", Cat = "Self-Help", Price = 14.99, PageNum = 304 }, new Book { Title = "It's Your Ship", AuthorFirst = "Michael", AuthorLast = "Abrashoff", AuthorMiddle = "", Publisher = "Grand Central Publishing", ISBN = "978-1455523023", Class = "Non-fiction", Cat = "Self-Help", Price = 21.66, PageNum = 240 }, new Book { Title = "The Virgin Way", AuthorFirst = "Richard", AuthorLast = "Branson", AuthorMiddle = "", Publisher = "Portfolio", ISBN = "978-15919847984", Class = "Non-fiction", Cat = "Business", Price = 29.16, PageNum = 400 }, new Book { Title = "Sycamore Way", AuthorFirst = "John", AuthorLast = "Grisham", AuthorMiddle = "", Publisher = "Bantam", ISBN = "978-0553393613", Class = "Fiction", Cat = "Thrillers", Price = 15.03, PageNum = 642 }, new Book { Title = "Anna Karenina", AuthorFirst = "Leo", AuthorLast = "Tolstoy", AuthorMiddle = "", Publisher = "Russian Messenger", ISBN = "978-0141447231", Class = "Fiction", Cat = "Historical-fiction", Price = 25.18, PageNum = 684 }, new Book { Title = "Happiness Advantage", AuthorFirst = "Shawn", AuthorLast = "Achor", AuthorMiddle = "", Publisher = "Corwn Publishing", ISBN = "978-8746209454", Class = "Non-Fiction", Cat = "Self-Help", Price = 9.99, PageNum = 256 }, new Book { Title = "The Giver", AuthorFirst = "Lois", AuthorLast = "Lowry", AuthorMiddle = "", Publisher = "Houghton Mifflin Harcourt", ISBN = "978-1328575487", Class = "Fiction", Cat = "Thrillers", Price = 7.83, PageNum = 192 } ); context.SaveChanges(); } }