0

I'm learning asp.net WebApi and EFCore (CodeFirst) and as an exercise, I'm building Warehouse Api and my update method doesn't work. This is my repository code:

public void Update(T toUpdate)
        {
            if(toUpdate == null) throw new ArgumentNullException("Entity is null");

            T tmp = GetById(toUpdate.Id);
            tmp = toUpdate;
            _context.SaveChanges();
        }

and this is my Service code:

public void UpdateEmployee(UpdateEmployeeCommand command)
        {
            UpdateEmployeeCommandValidator validator = new UpdateEmployeeCommandValidator();
            var results = validator.Validate(command);
            if (!results.IsValid)
            {
                throw new CommandValidationException(results.Errors.Select(x => new CommandValidationError
                {
                    ErrorCode = x.ErrorCode,
                    ErrorMessage = x.ErrorMessage,
                    PropertyName = x.PropertyName
                }));
            }
            _repository.Update(new Employee()
            {
                Id = command.Id,
                FirstName = command.FirstName,
                Address = command.Address,
                LastName = command.LastName,
                Age = command.Age,
                Email = command.Email,
                PhoneNumber = command.PhoneNumber
            });
        }

and this is how I use it in Controller:

public ActionResult UpdateEmployee(int Id, UpdateEmployeeCommand command)
        {
            if(Id != command.Id)
            {
                return BadRequest();
            }
            var employeeModelFromRepo = _repository.GetById(Id);
            if(employeeModelFromRepo == null)
            {
                return NotFound();
            }

            _employeeService.UpdateEmployee(command);

            return NoContent();
        }

When I call UpdateEmployee, it runs without any error but it doesn't update my database.

I'm new to this, so this might be an easy fix.

3
  • You need to copy new values to tmp, not just reassign the variable. Commented Jan 16, 2021 at 10:17
  • Try `db.Entry(tmp).CurrentValues.SetValues(toUpdate) Commented Jan 16, 2021 at 10:21
  • 1
    @GuruStron db.Update(toUpdate) gives me an error but db.Entry(tmp).CurrentValues.SetValues(toUpdate) work just fine. Commented Jan 16, 2021 at 10:48

3 Answers 3

1

I am using this generic update function:

public virtual T Update(T t) where T : class, IBaseEntity // contains Id as primary key
        {
            if (t == null)
                return null;
            var exist = Context.Set<T>().Find(t);

          // Or you can try 
           var exist = Context.Set<T>()
                    .Where(i=>i.Id=t.Id).FirstOrdDefault();

            if (exist == null) return exist;
            Context.Entry(exist).CurrentValues.SetValues(t);
            Context.SaveChanges();

            return exist;
        }
Sign up to request clarification or add additional context in comments.

Comments

0

Don't you forget to call your service method in the controller end-point? UpdateEmployee()

1 Comment

I've done that the first time I tried updating the database but it didn't work.
0

Commented solution works, i just had to add db.Entry(tmp).CurrentValues.SetValues(toUpdate) to the repository code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.