コード例 #1
0
        public async Task <object> Save([FromBody] UserModel model)
        {
            object result = null; string message = "";

            if (model == null)
            {
                return(BadRequest());
            }
            using (_ctx)
            {
                using (var _ctxTransaction = _ctx.Database.BeginTransaction())
                {
                    try
                    {
                        if (model.id > 0)
                        {
                            var entityUpdate = _ctx.User.FirstOrDefault(e => e.Id == model.id);
                            if (entityUpdate != null)
                            {
                                entityUpdate.FirstName = model.firstName;
                                entityUpdate.LastName  = model.lastName;
                                entityUpdate.Phone     = model.phone;
                                entityUpdate.Email     = model.email;
                                await _ctx.SaveChangesAsync();
                            }
                        }
                        else
                        {
                            User user = new User {
                                FirstName = model.firstName,
                                LastName  = model.lastName,
                                Email     = model.email,
                                Phone     = model.phone
                            };
                            _ctx.User.Add(user);
                            await _ctx.SaveChangesAsync();
                        }
                        _ctxTransaction.Commit();
                        message = "Saved Successfully";
                    }
                    catch (Exception ex)
                    {
                        _ctxTransaction.Rollback();
                        ex.ToString();
                        message = "Saved Error";
                    }
                    result = new { message };
                }
            }
            return(result);
        }
コード例 #2
0
        public async Task <IActionResult> PutLego([FromRoute] int id, [FromRoute] Lego lego)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id != lego.SerialNumber)
            {
                return(BadRequest());
            }

            _context.Entry(lego).State = EntityState.Modified;

            try
            {
                _repo.Update(lego);
                var save = await _repo.SaveAsync(lego);

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LegoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }