コード例 #1
0
        public async Task <IActionResult> Create([Bind("ID,Name")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(product));
        }
コード例 #2
0
ファイル: Repository.cs プロジェクト: pullig/Commerce
        public async Task <TEntity> AddAsync(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException($"{nameof(AddAsync)} entity must not be null");
            }

            try
            {
                await context.Set <TEntity>().AddAsync(entity);

                await context.SaveChangesAsync();

                return(entity);
            }
            catch (Exception ex)
            {
                throw new Exception($"{nameof(entity)} could not be saved: {ex.Message}");
            }
        }