public static BookSellerContext GetBookSellerContext(string dbName, int authorCount = 0, int bookCount = 0, int reviewCount = 0) { // Create options for DbContext instance var options = new DbContextOptionsBuilder <BookSellerContext>() .UseInMemoryDatabase(databaseName: dbName) .Options; // Create instance of DbContext var dbContext = new BookSellerContext(options); // Add entities in memory return(dbContext.Seed(authorCount, bookCount, reviewCount)); }
public static BookSellerContext Seed(this BookSellerContext dbContext, int authorCount = 0, int bookCount = 0, int reviewCount = 0) { //Load context with N Authors with random values for (int i = 1; i <= authorCount; i++) { dbContext.Author.Add(new Author { FirstName = RandomStrings(1).First(), LastName = RandomStrings(1).First() }); } dbContext.SaveChanges(); //Load context with N Books of the first author for (int i = 1; i <= bookCount; i++) { dbContext.Book.Add(new Book { Author = dbContext.Author.FirstOrDefault() ?? throw new Exception("Can't seed Books without any Author."), Price = Math.Round(Convert.ToDecimal(random.NextDouble()), 2) + 1, Title = string.Join(' ', RandomStrings(3)), });
public ReviewsController(BookSellerContext context) : base(context) => this.dbSet = context.Review;
public BooksController(BookSellerContext context) : base(context) => this.dbSet = context.Book;
public BaseController(BookSellerContext context) => this.context = context;
public AuthorsController(BookSellerContext context) : base(context) => dbSet = context.Author;