public static Book getBook(int bookID) { Book book; using (TestDbContext db = new TestDbContext()) { //book db depends on course db if (db.Courses.Count() == 0) { CourseUtils.initCourseDb(db); } //initialize book table if needed if (db.Books.Count() == 0) { initBookDb(db); } book = db.Books.Find(bookID); if (book == null) { //no book with the given ID, report error? } else { //This line is needed because LINQ requests are lazily loaded and the //TestDbContext was being destroyed before the Course was being resolved. //The same thing will need to be done for all foreign key lookups. book.Course = book.Course; } } return(book); }
public static Listing getListing(int listingID) { Listing listing; using (TestDbContext db = new TestDbContext()) { //book db depends on course db if (db.Courses.Count() == 0) { CourseUtils.initCourseDb(db); } //initialize book table if needed if (db.Books.Count() == 0) { BookUtils.initBookDb(db); } //initialize listing table if needed if (db.Listings.Count() == 0) { initListingDb(db); } listing = db.Listings.Find(listingID); if (listing != null) { //due to lazy loading, resolve all foreign key references //listing.UserProfile = listing.UserProfile; listing.Book = listing.Book; listing.Book.Course = listing.Book.Course; } else { //no listing with the given ID, report error? } } return(listing); }