AutoMapper does not map sub property

I am getting an issue with automapper v6.0.2 where automapper tries to drag and drop ICollection

in int

instead of the specified collection.

Mistake:

    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 configuration

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

      

Depending on:

        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 deleting dependent subscribers, subscriber cards are fine

As for the Dependent

Entity and Model

, they copy and paste all the elements public

. It seems like AutoMapper is trying to shrink the collection into the int

specified map instead.

Any ideas?

Dependents

is a navigation property

Edit: This was tested in a functional test in an async method. A quick unit test was 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);
}

      

The error returned now is the 'Add' method on type X has no 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 a dependent

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 a unit test work, although it doesn't actually use Automapper, which is a big caveat, however it still breaks on a functional test

I can't think of anything else to add as the dependent doesn't even have an integer field

Addendum: entity, Dto and caller are all in different libraries, but I was unable to replicate them in .NETFiddle

+3


source to share


2 answers


Couldn't figure out the root cause of the problem, but found a fix.

Changed property name MemberDto

Dependents

to Dependentss

and it worked. Changing it back tears it up again. I must add that there is no other Dependents property on any object.



I have no idea, but it looks like this is what was breaking it.

0


source


Automapper should usually handle collections for you. It will also look for any known cards that might apply, so it doesn't need to be called Mapper.Map

inside a MapFrom

.

Just define maps for individual objects:



CreateMap<Dependent, DependentDetailDto>();
CreateMap<Member, MemberDetailDto>()
    .ForMember(dto => dto.Dependents, o => o.MapFrom(src => src.Dependents);

      

+2


source







All Articles