Googli
Googli

Reputation: 15

Can't Update database with Entity Framework Core

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.

Upvotes: 0

Views: 996

Answers (3)

Serge
Serge

Reputation: 43959

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;
        }

Upvotes: 1

Googli
Googli

Reputation: 15

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

Upvotes: 0

crucifery
crucifery

Reputation: 458

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

Upvotes: 0

Related Questions