2

I have a problem like this. But this answer it didn't work for me.

I am working on a little project. I have two domain model (Post, Source):

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

But I am getting an error like that:

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

Source domain object and SourceViewModel similar Post and PostViewModel.

Hopefully this explains my problem. How can I resolve to this problem?

Community
  • 1
  • 1
mstfcck
  • 721
  • 3
  • 11
  • 28
  • 1
    Have you read the exception message? They usually explain the exact problem in some detail. – Charles Mager May 09 '15 at 14:47
  • Of course I read. The problem is relation of between Post and Source. I mean, when I create mapping for the other connected models the problem is solved. But in this time I have waited for the mapping almost 10 minutes and I broke in half to process. The process takes too long. Could be causing this? – mstfcck May 09 '15 at 19:30
  • You need to include the message in your question, then. And include enough code that someone would be able to reproduce the issue - saying the rest is "similar" isn't enough. – Charles Mager May 10 '15 at 09:09
  • One real quick thing - all of those MapFrom's are not necessary. You only need those if the names _don't_ match. Otherwise it's just pointless code. – Jimmy Bogard May 11 '15 at 18:27

3 Answers3

1

In my experience the most likely problem is in your sources collection.

When you want to use the automapper best practice is to explicitly define and register a map between your two entities, Also, make sure you have defined a map between the entity and the view model of the sub collection of sources.

Adam Diament
  • 4,290
  • 3
  • 34
  • 55
0

I created a new AutoMapperConfiguration for my mappings and I mapped all domain and view models.

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

And I registered to this configuration on 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();
    }
}

Now, the process takes too long on here:

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

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

        return View(model);
    }

I have waited almost 10 minutes... What is the problem? :|

mstfcck
  • 721
  • 3
  • 11
  • 28
0
        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();
mstfcck
  • 721
  • 3
  • 11
  • 28