AutoMapper:'AutoMapper.AutoMapperMappingException' 类型的异常发生在 AutoMapper.dll 中,但未在用户代码中处理

AutoMapper: An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code

我有类似 this. 的问题,但这个答案对我不起作用。

我正在做一个小项目。我有两个域模型(Post,来源):

public class Post
{
    public Post()
    {
        Sources = new HashSet<Source>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public string About { get; set; }
    public bool IsPublished { get; set; }
    public bool IsFinished { get; set; }
    public DateTime? CreatedOn { get; set; }
    public int? CreatedBy { get; set; }

    public virtual ICollection<Source> Sources { get; set; }
}

public class PostViewModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string About { get; set; }
    public bool IsPublished { get; set; }
    public bool IsFinished { get; set; }
    public DateTime? CreatedOn { get; set; }
    public int? CreatedBy { get; set; }

    public virtual ICollection<SourceViewModel> Sources { get; set; }
}   

public ActionResult Edit(int id)
{
    Mapper.CreateMap<Post, PostViewModel>()
        .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
        .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title))
        .ForMember(dest => dest.About, opt => opt.MapFrom(src => src.About))
        .ForMember(dest => dest.IsPublished, opt => opt.MapFrom(src => src.IsPublished))
        .ForMember(dest => dest.IsFinished, opt => opt.MapFrom(src => src.IsFinished))
        .ForMember(dest => dest.CreatedOn, opt => opt.MapFrom(src => src.CreatedOn))
        .ForMember(dest => dest.CreatedBy, opt => opt.MapFrom(src => src.CreatedBy))
        .ForMember(dest => dest.Sources, opt => opt.MapFrom(src => src.Sources))
        .ReverseMap();

    var Post = _PostService.Get(id);

    var model = Mapper.Map<Post, PostViewModel>(Post);

    return View(model);
}

但我收到这样的错误:

An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code

源域对象和SourceViewModel类似PostPostViewModel

希望这能解释我的问题。我该如何解决这个问题?

根据我的经验,最有可能的问题出在您的资源集合中。

当您想要使用自动映射器时,最佳做法是在您的两个实体之间显式定义和注册一个映射,另外,请确保您已经在实体和源子集合的视图模型之间定义了一个映射。

我为我的映射创建了一个新的 AutoMapperConfiguration 并映射了所有域和视图模型。

public class AutoMapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(map =>
        {
            map.AddProfile<MappingProfile>();
        });
    }
}

public class MappingProfile : Profile
{
    public override string ProfileName
    {
        get { return "MappingProfile"; }
    }

    protected override void Configure()
    {
        Mapper.CreateMap<Post, PostViewModel>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title))
            .ForMember(dest => dest.About, opt => opt.MapFrom(src => src.About))
            .ForMember(dest => dest.IsPublished, opt => opt.MapFrom(src => src.IsPublished))
            .ForMember(dest => dest.IsFinished, opt => opt.MapFrom(src => src.IsFinished))
            .ForMember(dest => dest.CreatedOn, opt => opt.MapFrom(src => src.CreatedOn))
            .ForMember(dest => dest.CreatedBy, opt => opt.MapFrom(src => src.CreatedBy))
            .ForMember(dest => dest.Sources, opt => opt.MapFrom(src => src.Sources))
            .ReverseMap();

        // The other models like above...
    }
}

我在 global.asax 上注册了此配置:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        DependencyResolver.SetResolver(new UnityDependencyResolver(Bootstrapper.Initialise()));

        //Configure AutoMapper
        AutoMapperConfiguration.Configure();
    }
}

现在,这个过程在这里花费的时间太长了:

    public ActionResult Edit(int id)
    {
        var series = _seriesService.Get(id);

        var model = Mapper.Map<Series, SeriesViewModel>(series); // here

        return View(model);
    }

我已经等了快10分钟了...有什么问题吗? :|

        Mapper.CreateMap<Post, PostViewModel>()
        .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
        .ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title))
        .ForMember(dest => dest.About, opt => opt.MapFrom(src => src.About))
        .ForMember(dest => dest.IsPublished, opt => opt.MapFrom(src => src.IsPublished))
        .ForMember(dest => dest.IsFinished, opt => opt.MapFrom(src => src.IsFinished))
        .ForMember(dest => dest.CreatedOn, opt => opt.MapFrom(src => src.CreatedOn))
        .ForMember(dest => dest.CreatedBy, opt => opt.MapFrom(src => src.CreatedBy))
        .ForMember(dest => dest.Sources, **opt => opt.Ignore()**)
        .ReverseMap();