Exemplo n.º 1
0
        // constructor - method use to create an instance of this class
        public ShopController(CtrlAltPcContext 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;
        }
Exemplo n.º 2
0
        public void TestInitialize()
        {
            // 1. use in-memory DbContext instead of connecting to SQL Server
            // similar to HTML local storage
            var options = new DbContextOptionsBuilder <CtrlAltPcContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            var _context = new CtrlAltPcContext(options);

            products = new List <Product>();

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

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

            products.Add(new Product
            {
                ProductId = 127,
                Name      = "Another Product",
                Price     = 19,
                Category  = mockCategory
            });
            // add each fake product to the list
            products.Add(new Product
            {
                ProductId = 194,
                Name      = "A Lousy Product",
                Price     = 4,
                Category  = mockCategory
            });

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

            // 3. this is the last step
            productsController = new ProductsController(_context);
        }
 public ProductsController(CtrlAltPcContext context)
 {
     _context = context;
 }
Exemplo n.º 4
0
 public CategoriesController(CtrlAltPcContext context)
 {
     _context = context;
 }
Exemplo n.º 5
0
 public OrdersController(CtrlAltPcContext context)
 {
     _context = context;
 }