我正在使用 AutoMapper,这是我更新现有客户详细信息的代码,但是当我更新一个字段时,其余字段将被清空。
public async Task<ResponseModel> Update(CustomerDTO customer, int id)
{
ResponseModel responseModel = new ResponseModel();
try
{
Customer? existingCustomer = await _dbContext.FindAsync<Customer>(id);
if (existingCustomer == null)
{
return new ResponseModel()
{
IsSuccess = false,
Message = $"Customer doesn't exist!"
};
}
_mapper.Map(customer, existingCustomer);
if (await _dbContext.SaveChangesAsync() > 0)
{
responseModel.Message = "Customer Updated Successfully";
responseModel.IsSuccess = true;
}
}
catch (Exception ex)
{
responseModel.IsSuccess = false;
responseModel.Message = $"Error:{ex.Message}";
}
return responseModel;
}
这是映射器配置文件:
using AutoMapper;
using static MGApi.Services.CustomerService;
namespace MGApi.Models
{
public class CustomerProfile : Profile
{
public CustomerProfile()
{
CreateMap<CustomerDTO, Customer>();
}
}
}
我尝试使用
ForAllOtherMembers
添加,但它已被弃用。我不确定如何解决这个问题。