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));
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.
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:
You can use reverse mapping for configure unflattening. Look at the official doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With