public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup <Startup>() .Build(); using (var db = new ECommContext()) { db.Database.Migrate(); } host.Run(); }
protected override void Up(MigrationBuilder migrationBuilder) { using (var db = new ECommContext()) { db.Products.AddRange( new Product() { Title = "iPhone", Price = 800, Rating = 5 }, new Product() { Title = "Pixel", Price = 700, Rating = 4 } ); db.SaveChanges(); } }
private ECommContext CreateStubContext() { var optionsBuilder = new DbContextOptionsBuilder <ECommContext>(); optionsBuilder.UseInMemoryDatabase(); var context = new ECommContext(optionsBuilder.Options); // Add sample data context.Products.Add(new Product { Id = 1, ProductName = "Milk", UnitPrice = 2.50M }); context.Products.Add(new Product { Id = 2, ProductName = "Bread", UnitPrice = 3.25M }); context.Products.Add(new Product { Id = 3, ProductName = "Juice", UnitPrice = 5.75M }); context.SaveChanges(); return(context); }
//we can use HomeController(IDistributedCache) //but it will not have Redis rich data type apis; like INCR public IActionResult Index() { var cache = Startup.RedisConnection.GetDatabase(); //await db.StringIncrementAsync("hitcounter"); cache.StringIncrement("hitcounter"); using (var db = new ECommContext()) { var products = (from p in db.Products where p.Rating > 3 select p).ToArray(); this.ViewData["Products"] = products; } var counter = cache.StringGet("hitcounter"); var visitors = cache.SetLength("visitor"); this.ViewData["counter"] = counter; this.ViewData["visitors"] = visitors; return(View()); }
public ProductController(ECommContext context) { _context = context; }
public ProductList(ECommContext context) { _context = context; }
public HomeController(ECommContext context) { _context = context; }
public ProductsApiController2(ECommContext context) { _context = context; }