2017-04-03 226 views
1

這裏是我的ViewModels和datamodelsautomapper錯誤映射類型集合

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;} 
} 

這裏是我的映射型材

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>>(); 

     } 

    } 

但是當我打電話

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

我得到

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: 

任何人都可以幫我解釋一下嗎?我在別處尋找類似的問題,並已實施解決方案,但注意到正在工作。

+0

你並不需要創建集合/列表明確的映射。從'PlayerProfile'中刪除這些行,它應該可以工作。 – Rabban

+0

謝謝@Rabban,我試過了,但我現在在同一個地方得到了一個堆棧溢出錯誤。任何想法爲什麼? – Jack

+0

「玩家」是否指向「聯盟」?也許有一個圓圈參考。 – Rabban

回答

0

由於拉班在評論中說,除明確收集轉換,然後加入

PreserveReferences() 

到我的個人資料解決了problem./