automapper类和嵌套类映射到一个类 [英] automapper class and nested class map to one class

查看:204
本文介绍了automapper类和嵌套类映射到一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了很多说明,但我想拍一张照片会使我的问题比文字更清楚

i have written alot of description but i figured making a picture will make my problem clearer than words

我已经将此代码写到地图上,但是它引发了异常

i have written this to map but it throws an exception

Mapper.CreateMap<GenericStory, GenericStoryDisplayViewModel>().ForMember(
            gs => gs.StoryBody,dest => dest.MapFrom( gs => gs));

尝试将StoryWriting.Web.Models.GenericStory映射到StoryWriting.Web.ViewModels.StoryBodyViewModel. 使用StoryWriting.Web.Models.GenericStory到StoryWriting.Web.ViewModels.GenericStoryDisplayViewModel的映射配置 目的地属性:StoryBody 缺少类型映射配置或不支持的映射. 抛出"AutoMapper.AutoMapperMappingException"类型的异常.

Trying to map StoryWriting.Web.Models.GenericStory to StoryWriting.Web.ViewModels.StoryBodyViewModel. Using mapping configuration for StoryWriting.Web.Models.GenericStory to StoryWriting.Web.ViewModels.GenericStoryDisplayViewModel Destination property: StoryBody Missing type map configuration or unsupported mapping. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.

推荐答案

我认为,使用AutoMapper,您还必须映射子类型,而不管它们是否包含在另一个映射类型中?

I thought with AutoMapper you had to map sub-types as well, regardless of if they were contained in another mapped type?

因此,在这种情况下,您将添加

So in this case you'd add

Mapper.CreateMap<GenericStory, StoryBodyViewlModel>();

,然后是您当前的映射.

and then your current mapping.

我已经更新了我的测试用例,甚至可以匹配您的图像,并且它可以按预期运行:

I've updated my test case to even match your images and it's functioning as expected:

public class GenericStory
{
    public string Description { get; set; }
    public int Id { get; set; }
    public bool IsFavoritedByCurrentUser { get; set; }
    public int StoryTypeId { get; set; }
    public string StoryTypeName { get; set; }
    public string Html { get; set; }
    public string Title { get; set; }
    public int TotalFavoritedByUsers { get; set; }
}

public class GenericStoryDisplayViewModel
{
    public string Description { get; set; }
    public int Id { get; set; }
    public int StoryTypeId { get; set; }
    public string StoryTypeName { get; set; }

    public StoryBodyViewModel StoryBody { get; set; }
}

public class StoryBodyViewModel
{
    public string Title { get; set; }
    public string Html { get; set; }

    public int TotalFavoritedByUsers { get; set; }
    public bool IsFavoritedByCurrentUser { get; set; }
}

然后是我的测试

private static void Main()
{
    var story = new GenericStory
    {
        Description = "Lorem ipsum dolor sit amet,....etc",
        Html = "<h1>ZOMG!</hl>\r\n\r\n<h2>BEES!</h2>",
        Id = 9,
        IsFavoritedByCurrentUser = true,
        StoryTypeId = 1,
        StoryTypeName = "ShortStory",
        Title = "Test Story",
        TotalFavoritedByUsers = 1
    };

    var vm = new GenericStoryDisplayViewModel();

    Mapper.CreateMap<GenericStory, StoryBodyViewModel>();
    Mapper.CreateMap<GenericStory, GenericStoryDisplayViewModel>()
       .ForMember(dest => dest.StoryBody, opt => opt.MapFrom(src => src));

    Mapper.Map(story, vm);

    Console.ReadKey();
}

结果:

这篇关于automapper类和嵌套类映射到一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆