public async Task SaveProduct()
        {
            using (var context = new RestBuyContext(this.optionsBuilder.Options))
            {
                await context.Database.EnsureCreatedAsync();

                var product = new Product()
                {
                    Category    = "Electronics",
                    Description = "Smart Phone",
                    Name        = "iPhone",
                    PictureUri  = "apple.com",
                    Price       = 1000
                };
                await context.AddAsync(product);

                await context.SaveChangesAsync();
            }

            using (var context = new RestBuyContext(optionsBuilder.Options))
            {
                var product = await context.Products.FirstOrDefaultAsync(c => c.Name == "iPhone");

                Assert.IsNotNull(product);
                await context.Database.EnsureDeletedAsync();
            }
        }
        public async Task DeleteAProduct()
        {
            var product = new Product()
            {
                Category    = "Electronics",
                Description = "Smart Phone",
                Name        = "iPhone",
                PictureUri  = "apple.com",
                Price       = 1000
            };

            //Save the product as usual
            using (var context = new RestBuyContext(this.optionsBuilder.Options))
            {
                await context.Database.EnsureCreatedAsync();

                await context.AddAsync(product);

                await context.SaveChangesAsync();
            }

            using (var context = new RestBuyContext(this.optionsBuilder.Options))
            {
                //Get the product from db and check its existence
                var productFromDB = await context.Products.FindAsync(product.Id);

                Assert.IsNotNull(productFromDB);
                //delete the product
                context.Remove(productFromDB);

                await context.SaveChangesAsync();

                //recheck the product
                productFromDB = await context.Products.FindAsync(product.Id);

                //now the answer should be no

                Assert.IsNull(productFromDB);
                await context.Database.EnsureDeletedAsync();
            }
        }
Esempio n. 3
0
        public async Task SaveProduct()
        {
            var optionsBuilder = new DbContextOptionsBuilder <RestBuyContext>();
            var config         = new ConfigurationBuilder()
                                 .SetBasePath(Directory.GetCurrentDirectory())
                                 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                                 .Build();

            optionsBuilder.UseSqlServer(config.GetConnectionString("DefaultConnection"));

            //We add the product.
            using (var context = new RestBuyContext(optionsBuilder.Options))
            {
                await context.Database.EnsureCreatedAsync();

                var product = new Product()
                {
                    Category    = "Electronics",
                    Description = "Smart Phone",
                    Name        = "iPhone",
                    PictureUri  = "apple.com",
                    Price       = 1000,
                };
                await context.AddAsync(product);

                await context.SaveChangesAsync();
            }
            // Here we remove the product.
            using (var context = new RestBuyContext(optionsBuilder.Options))
            {
                var product = await context.Products.FirstOrDefaultAsync(c => c.Name == "iPhone");

                Assert.IsNotNull(product);
                await context.Database.EnsureDeletedAsync();
            }
        }