阅读背景:

在Automapper映射之后,对象转到空值

来源:互联网 
 public class CreateOrEditOwnerDetailInput : IInputDto
    {
        [Required]
        public OwnerDetailEditDto OwnerDetail { get; set; }
    }

    [AutoMap(typeof(OwnerDetail))]
    public class OwnerDetailEditDto
    {
        public const int MaxLength = 50;
        public const int NotesMaxLength = 300;

        public int? Id { get; set; }

        [Required]
        [MaxLength(MaxLength)]
        public string LastName { get; set; }


        [Required]
        [MaxLength(MaxLength)]
        public string CompanyName { get; set; }

        [Required]
        public OwnerContactDetailDto ContactDetail { get; set; }

        [Required]
        public AdditionalAddressDto AdditionalAddress { get; set; }

    }



 [Table("IpOwnerDetails")]
    public class OwnerDetail : FullAuditedEntity
    {
        public const int MaxLength = 50;
        public const int NotesMaxLength = 300;

        [Required]
        [MaxLength(MaxLength)]
        public virtual string LastName { get; set; }

        [Required]
        [MaxLength(MaxLength)]
        public virtual string CompanyName { get; set; }


        [ForeignKey("AdditionalAddressId")]
        public virtual AdditionalAddress AdditionalAddress { get; set; }
        public virtual int AdditionalAddressId { get; set; }

        [ForeignKey("ContactDetailId")]
        public virtual ContactDetail ContactDetail { get; set; }
        public virtual int ContactDetailId { get; set; }


    }

public class OwnerContactDetailDto : FullAuditedEntityDto
    {
        public const int NumberMaxLength = 20;

        [Required]
        [MaxLength(NumberMaxLength)]
        public string MainPhoneNumber { get; set; }

        [MaxLength(NumberMaxLength)]
        public string HomePhoneNumber { get; set; }

        [Required]
        public ContactDetailType Type { get; set; }
    }

  public class AdditionalAddressDto : FullAuditedEntityDto, IOutputDto
    {
        public const int MaxLength = 50;

        [Required]
        [MaxLength(MaxLength)]
        public string StreetNumber { get; set; }

        [Required]
        public AddressType Type { get; set; }

        [Required]
        public int CityId { get; set; }

        [Required]
        public int StateId { get; set; }


    }

Mapper.CreateMap<AdditionalAddress, AdditionalAddressDto>()
              .ReverseMap()
              .ForMember(additionalAddress => additionalAddress.Id, options => options.Ignore());

            Mapper.CreateMap<ContactDetail, OwnerContactDetailDto>()
              .ReverseMap()
              .ForMember(contactDetail => contactDetail.Id, options => options.Ignore());


 public async Task<int?> EditOwnerDetailAsync(CreateOrEditOwnerDetailInput input)
        {
            var ownerDetail = await _ownerDetailRepository.FirstOrDefaultAsync(p => p.Id == input.OwnerDetail.Id);
            input.OwnerDetail.MapTo(ownerDetail);//after this it goes null
            await _ownerDetailRepository.UpdateAsync(ownerDetail);
            return input.OwnerDetail.Id;

        }
 public class CreateOrEditOwnerDetailInput : II



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: