automapper error mapping types of collection

automapper c# entity-framework-6 mapping

Question

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.

1
1
4/3/2017 10:04:50 AM

Accepted Answer

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

PreserveReferences()

to my profile solved the problem./

1
4/3/2017 2:45:04 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow