c# - 自动映射器错误映射集合类型

标签 c# entity-framework-6 mapping automapper

这是我的 View 模型和数据模型

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

这是我的 map 配置文件

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: 

有人可以帮我解释一下吗?我在其他地方寻找过类似的问题并实现了解决方案,但注意到是有效的。

最佳答案

正如 Rabban 在评论中所说,删除显式集合转换,然后添加

PreserveReferences()

到我的个人资料解决了问题。/

关于c# - 自动映射器错误映射集合类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43181750/

相关文章:

c# - syncfusion winrt图表

grails - grails 是如何实现处理程序映射的?

java - 如何从 Java 中的泛型类型反序列化 JSON 对象?

c# - Entity Framework 审计一对多关系并捕获相关实体

c# - Entity Framework 6 - 引用表 - 如何编写自定义导航属性逻辑?

ruby-on-rails - 为什么多字段映射不适用于 elasticsearch 的轮胎 gem ?

c# - 选择具有特定数量的嵌套文档项目的文档

c# - 通过 HasValue = false 的反射创建 Nullable,仅给定类型为 `Type` 的参数

c# - 需要帮助将此方法(使用 string.Join() 和 LINQ)变成通用方法

c# - 在或包含查询使用 System.Linq.Dynamic