2

Here are my viewmodels and datamodels

public class League{
    ICollection<Player> Players{get;set;}
}

public class Player{
    string name{get;set;}
}

public class LeagueViewModel{
    ICollection<PlayerViewModel> Players{get;set;}
}

public class PlayerViewModel{
    string name{get;set;}
}

And here are my mapping profiles

public class LeagueProfile : Profile
{
    public LeagueProfile()
    {
        CreateMap<League, LeagueModel>()
            .ForMember(m => m.Players, opt => opt.MapFrom(c => c.Players));

        CreateMap<LeagueModel, League>()
            .ForMember(m => m.Players, opt => opt.MapFrom(c => c.Players));
    }
}

 public class PlayerProfile : Profile
    {
        public PlayerProfile()
        {
            CreateMap<PlayerModel, Player>().ForMember(c => c.Parent, opt => opt.MapFrom(s => s.Parent));
            CreateMap<Player, PlayerModel>().ForMember(c => c.Parent, opt => opt.MapFrom(s => s.Parent));

            CreateMap<ICollection<Player>, ObservableCollection<PlayerModel>>();
            CreateMap<ObservableCollection<PlayerModel>, ICollection<Player>>();

        }

    }

but when I call

league.Players = mapper.Map(model.Players,league.Players);

I get

AutoMapper.AutoMapperMappingException was unhandled by user code
  HResult=-2146233088
  Message=Error mapping types.

Mapping types:
LeagueModel -> League
UI.Infrastructure.Models.LeagueModel -> DataLayer.Entities.League

Type Map configuration:
LeagueModel -> League
UI.Infrastructure.Models.LeagueModel -> DataLayer.Entities.League

Property:
Players
  Source=Anonymously Hosted DynamicMethods Assembly
  StackTrace:
       at lambda_method(Closure , LeagueModel , League , ResolutionContext )
       at UI.Infrastructure.Services.GenerateRoundService.ConstructRounds() in D:\TFS\TournamentSoftware\UI.Infrastructure\Services\GenerateRoundService.cs:line 29
       at UI.Infrastructure.Models.LeagueModel.ConstructRounds() in D:\TFS\TournamentSoftware\UI.Infrastructure\Models\LeagueModel.cs:line 108
       at UI.Infrastructure.Models.LeagueModel.GenerateRounds() in D:\TFS\TournamentSoftware\UI.Infrastructure\Models\LeagueModel.cs:line 86
       at UI.LeagueModule.ViewModels.LeagueViewModel.GenerateRounds() in D:\TFS\TournamentSoftware\UI.LeaugeModule\ViewModels\LeagueViewModel.cs:line 91
       at Prism.Commands.DelegateCommand.<>c__DisplayClass1_0.<.ctor>b__0(Object o)
       at Prism.Commands.DelegateCommandBase.<>c__DisplayClass6_0.<.ctor>b__0(Object arg)
       at Prism.Commands.DelegateCommandBase.<Execute>d__15.MoveNext()
  InnerException: 
       HResult=-2146233054
       Message=Method 'Add' in type 'Proxy<System.Collections.Generic.ICollection`1[[DataLayer.Entities.Player_DataLayer_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.
       Source=mscorlib
       TypeName=Proxy<System.Collections.Generic.ICollection`1[[DataLayer.Entities.Player_DataLayer_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null]]_mscorlib_Version=4.0.0.0_Culture=neutral_PublicKeyToken=b77a5c561934e089>
       StackTrace:
            at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
            at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
            at System.Reflection.Emit.TypeBuilder.CreateType()
            at AutoMapper.Execution.ProxyGenerator.EmitProxy(Type interfaceType)
            at AutoMapper.LockingConcurrentDictionary`2.<>c__DisplayClass2_1.<.ctor>b__1()
            at System.Lazy`1.CreateValue()
            at System.Lazy`1.LazyInitValue()
            at System.Lazy`1.get_Value()
            at AutoMapper.Execution.ProxyGenerator.GetProxyType(Type interfaceType)
            at lambda_method(Closure , LeagueModel , League , ResolutionContext )
       InnerException: 

Can anyone help me shed some light on this? I've looked for a similar issue elsewhere and have implemented the solutions but noting is working.

Jack
  • 131
  • 1
  • 9
  • You don't need to create explicit mappings for collections/lists. Remove these lines from `PlayerProfile` and it should work. – Rabban Apr 03 '17 at 10:14
  • Thanks @Rabban, I tried but I now get a stack overflow error at the same place. Any ideas as to why? – Jack Apr 03 '17 at 10:18
  • Do the `Player` points to the `League`? Maybe there is a circle reference. – Rabban Apr 03 '17 at 10:20
  • Yeah they do, thats the Parent mapping on the PlayerProfile. – Jack Apr 03 '17 at 10:22
  • 1
    Look [here](http://stackoverflow.com/q/40650571/6666799), it should solve your problem. – Rabban Apr 03 '17 at 10:50
  • Thanks @Rabban that has fixed my problem! – Jack Apr 03 '17 at 10:55
  • @Rabban would you be able to shed any light on a previous question I asked? http://stackoverflow.com/questions/43136015/using-automapper-to-map-objects-within-constructor I think im going to have to take the parent object out of the constructor to make it work, but im not sure how to resolve the IMapper requirement. – Jack Apr 03 '17 at 11:54

1 Answers1

2

As Rabban said in the comments removing the explicit collection conversion and then adding

PreserveReferences()

to my profile solved the problem./

Jack
  • 131
  • 1
  • 9