简体   繁体   中英

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM