Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper not mapping sub property

I am receiving an issue with automapper v6.0.2 where automapper is trying to shove an ICollection into an int instead of the specified collection.

Error:

    Mapping types:
Member_B3B146AB4F61AEDE3DF3639BDBD65BFA5BCA0FC414F11960DC39DA834F2A8CBD -> ICollection`1
System.Data.Entity.DynamicProxies.Memberr_B3B146AB4F61AEDE3DF3639BDBD65BFA5BCA0FC414F11960DC39DA834F2A8CBD -> System.Collections.Generic.ICollection`1[[MemberService.Dto.DependentDetailDto, MemberService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

Destination path:
MemberDetailDto.Dependents.Dependents

Source value:
System.Data.Entity.DynamicProxies.MbmRosterSubscriber_B3B146AB4F61AEDE3DF3639BDBD65BFA5BCA0FC414F11960DC39DA834F2A8CBD ---> System.OverflowException: Value was either too large or too small for an Int32.

Member

public Member ()
{
   this.Dependents = new HashSet<Dependent>();
}
public virtual ICollection<Dependent> Dependents { get; set; }

AutoMapper Config

CreateMap<Dependent, DependentDetailDto>();
CreateMap<Member, MemberDetailDto>()
                .ForMember(a => a.Dependents, o => o.MapFrom(x => Mapper.Map<ICollection<Dependent>, ICollection<DependentDetail>>(x.Dependents)));

Dependent:

        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
        public System.DateTime Dob { get; set; }
        public string Sex { get; set; }
        public string Relationship { get; set; }
        public string CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
        public string DepartmentCode { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string Address3 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string MailCountry { get; set; }
        public string Zip { get; set; }
        public Nullable<System.DateTime> SsnEncryptDate { get; set; }

When Dependents is removed Subscriber maps perfectly

As for the Dependent Entity and Model they are copy and paste with all items public. It appears as if AutoMapper is trying to squeeze the collection into an int instead of the specified map.

Any Ideas?

Dependents is a navigation property

Edit : This was being tested in a functional test in an async method. Quick unit test has been created:

[TestMethod]
public  void RetrieveActiveMember()
{
    MemberEntity.Member Subscriber = new Member();
    ICollection<Dependent> Dependent = new List<Dependent>();
    Dependent.Add(new Dependent { HostCountry = "HOME" });
    Subscriber.Dependents = Dependent;

    var Mapped = AutoMapper.Mapper.Map<Member, MemberDetailDto>(Subscriber);

    Assert.IsNotNull(Mapped);
}

Error being returned now is Method 'Add' in type X does not have an implementation'

Property:
Dependents ---> System.TypeLoadException: Method 'Add' in type 'Proxy<System.Collections.Generic.ICollection`1[[MemberService.Dto.DependentDetailDto_MemberService_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null]]_mscorlib_Version=4.0.0.0_Culture=neutral_PublicKeyToken=b77a5c561934e089>' from assembly 'AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005' does not have an implementation.

Changing The automapper config for the dependent to

CreateMap<ICollection<Dependent>, ICollection<DependentDetailDto>>()
                .ConstructUsing((ICollection<Dependent> Dependents) => {
                    List<DependentDetailDto> DependentList = new List<DependentDetailDto>();
                    foreach (var Dependent in Dependents)
                        DependentList.Add(new DependentDetailDto { });
                return DependentList;
            });

Makes the unit test work albeit no longer actually using Automapper which is a big caveat, however it still breaks on the functional test

I can't think of anything else to add as Dependents does not even have an integer field

Addendum: The entity, the Dto, and the caller are all in different libraries but I have not been able to replicate it in .NETFiddle

like image 712
SCFi Avatar asked Feb 27 '26 07:02

SCFi


1 Answers

Automapper should normally handle collections for you. It will also look up any known maps that might apply, so no need to call Mapper.Map inside a MapFrom.

Just define the maps for individual entities:

CreateMap<Dependent, DependentDetailDto>();
CreateMap<Member, MemberDetailDto>()
    .ForMember(dto => dto.Dependents, o => o.MapFrom(src => src.Dependents);
like image 147
Georg Patscheider Avatar answered Feb 28 '26 21:02

Georg Patscheider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!