コード例 #1
0
        // constructor - method to create an instance of this class
        // Dependency Injection
        public ShopController(toprockContext context, IConfiguration configuration)
        {
            // accept in an instance of our db connection class and use this object to connect
            _context = context;

            // accept an instance of the configuration object so we can read appsettings
            _configuration = configuration;
        }
        public void TestInitialize()
        {
            var options = new DbContextOptionsBuilder <toprockContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            _context = new toprockContext(options);
            products = new List <Product>();

            // 2. create mock data and add to in-memory database
            Category mockCategory = new Category
            {
                CategoryId = 100,
                Name       = "A Fake Category"
            };

            products.Add(new Product
            {
                ProductId = 1,
                Name      = "Product A",
                Price     = 10,
                Category  = mockCategory
            });

            products.Add(new Product
            {
                ProductId = 2,
                Name      = "Product B",
                Price     = 5,
                Category  = mockCategory
            });

            products.Add(new Product
            {
                ProductId = 3,
                Name      = "Product C",
                Price     = 15,
                Category  = mockCategory
            });

            foreach (var p in products)
            {
                // add each product to in-memory db
                _context.Product.Add(p);
            }
            _context.SaveChanges();


            productsController = new ProductsController(_context);
        }
コード例 #3
0
 public ProductsController(toprockContext context)
 {
     _context = context;
 }
コード例 #4
0
 public CategoriesController(toprockContext context)
 {
     _context = context;
 }
コード例 #5
0
 public OrdersController(toprockContext context)
 {
     _context = context;
 }