Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automapper class and nested class map to one class

Tags:

automapper

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

The mapping process

i have written this to map but it throws an exception

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

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.

like image 247
Nadeem Khedr Avatar asked May 12 '11 21:05

Nadeem Khedr


2 Answers

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.

EDIT:

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

and then my test

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

Results:

Results

like image 72
Khepri Avatar answered Sep 28 '22 06:09

Khepri


You can use reverse mapping for configure unflattening. Look at the official doc

like image 22
Andrei Avatar answered Sep 28 '22 08:09

Andrei