Я использую AutoMapper
(для сопоставления Entity to Dto
), C # и ef core 3.0, все работает нормально
(например, добавление / обновление мастера и добавление нового дочернего элемента)
Но моя проблема до тех пор, пока Update
EXISTING
дочерняя сущность!Я пытался обновить существующий дочерний элемент, но если я оставляю пустое любое поле, все нулевые поля сохраняются с нулевым значением в моей БД.
(например, если изменить только FirstName, другие значения сохраняются как нулевые)
Любая проблема в моем коде?
Класс родительского объекта:
public class ClientInfo
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public GenderType Gender { get; set; }
public string IdentityNumber { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string Address { get; set; }
public string Email { get; set; } = null;
public string Country { get; set; }
public string Province { get; set; }
public string City { get; set; }
public bool IsActive { get; set; }
public ICollection<CompanionInfo> CompanionInfo { get; set; }
}
Класс дочернего объекта:
public class CompanionInfo
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public GenderType Gender { get; set; }
public string IdentityNumber { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string Address { get; set; }
public CompanionTypes CompanionType { get; set; }
public Guid ClientInfoId { get; set; }
public ClientInfo ClientInfo { get; set; }
}
класс Dto такой же, как у сущности!
Профиль AutoMapper:
public class ClientInfoApplicationAutoMapperProfile : Profile
{
public ClientInfoApplicationAutoMapperProfile()
{
AllowNullCollections = true;
CreateMap<ClientInfo, ClientInfoDto>().ReverseMap()
.ForMember(d => d.CompanionInfo, m => m.MapFrom(s => s.CompanionInfo));
CreateMap<ClientInfo, CreateUpdateClientInfoDto>().ReverseMap()
.ForMember(d => d.CompanionInfo, m => m.MapFrom(s => s.CompanionInfo))
.ForAllMembers(opt => opt.Condition((s, d, m) => m != null));
CreateMap<CompanionInfo, CompanionInfoDto>().ReverseMap()
.ForAllMembers(opt => opt.Condition((s, d, m) => m != null));
}
}