简体   繁体   中英

Update method - Value cannot be null. (Parameter 'entity')

I have a problem that I cannot solve. When Im trying to update one field in the db called 'Introduction', I get this message from API:

System.ArgumentNullException: Value cannot be null. (Parameter 'entity') at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName) at Microsoft.EntityFrameworkCore.DbContext.Entry[TEntity](TEntity entity) at ClinicAPIv1.Data.UserRepository.Update(ClinicUser clinicUser) in E:\Users\User\Desktop\Clinic\ClinicAPIv1\ClinicAPIv1\Data\UserRepository.cs:line 61 at ClinicAPIv1.Controllers.UsersController.UpdateDoctor(DoctorUpdateDTO doctorUpdateDTO) in E:\Users\User\Desktop\Clinic\ClinicAPIv1\ClinicAPIv1\Controllers\UsersController.cs:line 74 at lambda_method34(Closure, Object ) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Logged|12_1(ControllerActionInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.Co ntrollerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.As pNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at ClinicAPIv1.Middleware.ExceptionMiddleware.InvokeAsync(HttpContext context) in E:\Users\User\Desktop\Clinic\ClinicAPIv1\ClinicAPIv1\Middleware\ExceptionMiddleware.cs:line 30

I understand that the problem is with the update function in my repository:

public void Update(ClinicUser clinicUser)
    {
        _context.Entry(clinicUser).State = EntityState.Modified;
    }

line 61 is

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

Controller Put method looks like:

   [HttpPut]
public async Task<ActionResult> UpdateDoctor(DoctorUpdateDTO doctorUpdateDTO)
    {
        var email = User.FindFirst(ClaimTypes.Name)?.Value;
        var user = await _userRepository.GetUserByEmail(email);

        _mapper.Map(doctorUpdateDTO, user);

        _userRepository.Update(user);

        if (await _userRepository.SaveAllAsync()) return NoContent();

        return BadRequest("Failed to update Doctor Profile :(");
    }

Line 74 is

_userRepository.Update(user);

DoctorUpdateDTO:

public class DoctorUpdateDTO
{
    public string Introduction { get; set; }
}

Mapper:

public class AutoMapperProfiles : Profile
{
    public AutoMapperProfiles()
    {
        CreateMap<ClinicUser, MemberDTO>()
            .ForMember(dest => dest.PhotoUrl, opt => opt.MapFrom(
                src => src.Photos.FirstOrDefault().Url));
        CreateMap<Photo, PhotoDTO>();
        CreateMap<DoctorUpdateDTO, ClinicUser>();
    }
}

GetUserByEmail:

public async Task<ClinicUser> GetUserByEmail(string email)
    {
        return await _context.clinicUsers
            .Where(x => x.TemporaryRole == "Doctor")
            .Include(p => p.Photos)
            .SingleOrDefaultAsync(x => x.Email == email);
    }

Please help me solve the problem

Chain ReverseMap after CreateMap calls in your AutoMapperProfiles. Get more information here .

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