简体   繁体   English

更新方法 - 值不能是 null。 (参数“实体”)

[英]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:当我尝试更新数据库中名为“简介”的字段时,我从 API 收到此消息:

System.ArgumentNullException: Value cannot be null. System.ArgumentNullException:值不能为 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 (参数“实体”)在 Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T 值,字符串参数名称)在 Microsoft.EntityFrameworkCore.DbContext.Entry[TEntity](TEntity 实体)在 ClinicAPIv1.Data.UserRepository.Update( ClinicUser ClinicUser) 在 E:\Users\User\Desktop\Clinic\ClinicAPIv1\ClinicAPIv1\Data\UserRepository.cs:line 61 at ClinicAPIv1.Controllers.UsersController.UpdateDoctor(DoctorUpdateDTO doctorUpdateDTO) 在 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.Co 的 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Logged|12_1(ControllerActionInvoker 调用程序) 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 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 。作为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 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) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) 在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) 在 ClinicAPIv1 .Middleware.ExceptionMiddleware.InvokeAsync(HttpContext context) 在 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:我知道问题出在我的存储库中的更新 function 上:

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

line 61 is第 61 行是

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

Controller Put method looks like: Controller Put 方法如下所示:

   [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第 74 行是

_userRepository.Update(user);

DoctorUpdateDTO:医生更新DTO:

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.在 AutoMapperProfiles 中调用 CreateMap 之后链 ReverseMap。 Get more information here . 在此处获取更多信息。

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

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