简体   繁体   English

无法使用 Entity Framework Core 更新数据库

[英]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.我正在学习 asp.net WebApi 和 EFCore (CodeFirst),作为练习,我正在构建仓库 Api,但我的更新方法不起作用。 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:这就是我在 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.当我调用 UpdateEmployee 时,它运行时没有任何错误,但它不会更新我的数据库。

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?你不会忘记在 controller 端点中调用你的服务方法吗? UpdateEmployee()

Commented solution works, i just had to add db.Entry(tmp).CurrentValues.SetValues(toUpdate) to the repository code.评论解决方案有效,我只需将db.Entry(tmp).CurrentValues.SetValues(toUpdate)添加到存储库代码中。

I am using this generic update function:我正在使用这个通用更新 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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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