6

I am using an Automapper to map one model to second model which has some rows with the same name as the first model. I am getting this inner exception

Missing type map configuration or unsupported mapping.
Mapping types: Bed -> Bed Model1.Bed -> Model2.Bed

This is the condensed version of the code.

Model 1

public class Model1
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<Bed> Beds { get; set; }
    public IEnumerable<Bed> Beds1 { get; set; }
    public string Status {get; set;}
    public string Notes {get; set;} 
}
public class Model2
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<Bed> Beds { get; set; }
    public IEnumerable<Bed> Beds1 { get; set; }
}


public class Bed
{
    public Guid Id { get; set; }
    public Guid TypeId { get; set; }
    public string Type { get; set; }        
    public string Notes { get; set; }
    public DateTime? CreatedOn { get; set; }
    public DateTime? LastUpdatedOn { get; set; }
}

Mapping :

 MapperConfiguration config = null;
 config = new MapperConfiguration(cfg => { cfg.CreateMap<Model1, Model2>(); });           
 IMapper mapper = config.CreateMapper();
 var dest = mapper.Map<Model1, Model2>(update);

update is an instance of Model1. Am I missing something? Should I do something with regards to the Bed class mapping even though both reference the same method? Also is there a way to copy data from one model to another in this scenario other than manually doing it or using Automapper?

EDIT 1: I am sure the error is from that mapping. I removed it and all the other fields were fine and there was no exception. But I am not sure how I should do the Bed -> Bed binding.

EDIT 2:This link seems to have the same issue and they say it's a possible bug and This one seems to have the answer although I am not sure how I can adapt it to my situation.

3
  • Have you tried creating a mapping for Bed -> Bed? Seems like it shouldn't be necessary but perhaps worth a shot... Commented Jun 8, 2017 at 17:28
  • @Zoop How do I go about doing it? What part of my code do I add it? Commented Jun 8, 2017 at 18:35
  • I'm not positive, but I'd try adding a second line in between the curly braces {}. In other words: config = new MapperConfiguration(cfg => { cfg.CreateMap<Model1, Model2>(); cfg.CreateMap<Bed, Bed>(); }); Commented Jun 8, 2017 at 18:59

1 Answer 1

4

It seems like auto mapper is having trouble determining how to map Bed. I would try the following:

config = new MapperConfiguration(cfg => { 

     cfg.CreateMap<Model1, Model2>(); 
     cfg.CreateMap<Bed, Bed>();

});

Although it seems odd that would be necessary...

Sign up to request clarification or add additional context in comments.

2 Comments

That was the missing issue. I am not sure why it was needed but it seem to resolve the problem.
Yeah, seems strange but I'm glad it worked. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.