public async Task <ProductDto> AddProductAsync(ProductDto product) { var newProduct = await _beerContext.AddAsync(_productTranslator.ToModel(product)); await _beerContext.SaveChangesAsync(); var dbProduct = await _beerContext.Products.AsNoTracking().FirstOrDefaultAsync(p => p.EAN == product.EAN); return(_productTranslator.ToDto(dbProduct)); }
private void Seed() { using var context = new BeerContext(ContextOptions); context.Database.EnsureDeleted(); context.Database.EnsureCreated(); ProductDto one = new ProductDto { ProductNo = "P001", Category = "Beer", EAN = "1", Name = "Name 1 Beer" }; ProductDto two = new ProductDto { ProductNo = "P002", Category = "Beer", EAN = "1", Name = "Name 2 Beer" }; ProductDto three = new ProductDto { ProductNo = "P003", Category = "Beer", EAN = "1", Name = "Name 3 Beer" }; var productTranslator = new ProductTranslator(); context.Products.AddRange(productTranslator.ToModel(one), productTranslator.ToModel(two), productTranslator.ToModel(three)); context.SaveChanges(); }