1

I have two exactly same classes but in different namespace (i.e. project). Now I am mapping object of both classes using AutoMapper but it gives me an error. The error is:

"cannot convert lambda expression to type 'string' because it is not a delegate type"

Below is my code:

Mapper.CreateMap<Namespace1.EventID, Namespace2.EventID>().ForMember(
    dest => dest.EventType, src => src.EventType);

I have also tried this.

Mapper.CreateMap<Namespace1.EventID, Namespace2.EventID>().ForMember(
    dest => dest.EventType, map => map.MapFrom(src => src.Eventtype));

Namespace2.EventID destEventID = Mapper.Map<Namespace2.EventID>(eventID);

I am getting error at dest => dest.EventType. Note that EventType is of some other class type.

As both the class have same structure, ForMember() is not required but if I remove ForMember(), then it shows runtime exception "Missing type map configuration or unsupported mapping".

I have added system.Linq and system.Data both. How can I map both of these types?

2
  • Dont you need to specify a Map for that class also. Because automapper was not that smart when i used it. If there is an CreateMap for some Objects then it is working. Mar 4, 2014 at 12:25
  • What is EventType? If those are two different types that AutoMapper can't auto-map, you'll need to define that mapping too. Mar 5, 2014 at 18:39

2 Answers 2

4

I was getting the same error. From what you posted I don't know if our reasons are the same, but perhaps it helps explaining my error reason.

There is an overload with string as first parameter, but we need the other where the first parameter is a function. The compiler does not seem to get that. In my case, that was because the compiler had an error in the second argument. Once that was fixed, there was no complaining about "cannot convert lambda expression to type 'string' because it is not a delegate type" anymore.

Here's the stuff I work with:

public class ClassA
{
    public DateTime? memberX { get; set; }
    public DateTime? memberY { get; set; }
}
public class ClassB
{
    public long? memberX { get; set; }
    public long? memberY { get; set; }
}
DateTime IntToDateTime(int t);
int DateTimeToInt(DateTime t);

The first (CreateMap<ClassA,ClassB>) works, the second (CreateMap<ClassB,ClassA>) doesn't.

CreateMap<ClassA, ClassB>()
    .ForMember(dest => dest.memberX, opt => opt.MapFrom(src => src.memberX.HasValue ? DateTimeToInt(src.memberX.Value) : 0))
    .ForMember(dest => dest.memberY, opt => opt.MapFrom(src => src.memberY.HasValue ? DateTimeToInt(src.memberY.Value) : 0))
    ;
CreateMap<ClassB, ClassA>()
    .ForMember(dest => dest.memberX, opt => opt.MapFrom(src => src.memberX.HasValue ? IntToDateTime((int)src.memberX.Value) : null))
    .ForMember(dest => dest.memberY, opt => opt.MapFrom(src => src.memberY.HasValue ? IntToDateTime((int)src.memberY.Value) : null))
    ;

What does work for the second (CreateMap<ClassB,ClassA>) is this:

CreateMap<ClassB, ClassA>()
    .ForMember(dest => dest.memberX, opt => opt.MapFrom(src => src.memberX.HasValue ? IntToDateTime((int)src.memberX.Value) : (DateTime?)null))
    .ForMember(dest => dest.memberY, opt => opt.MapFrom(src => src.memberY.HasValue ? IntToDateTime((int)src.memberY.Value) : (DateTime?)null))
    ;

Are you getting any other compiler errors?

Possible solution:

Is your EventType class the same class, or is one a Namespace1.SomeClass EventType and the other a Namespace2.SomeClass EventType? If that's the case, you need to create a map for those, first. Then it should work without .ForMember.

Mapper.CreateMap<Namespace1.SomeClass, Namespace2.SomeClass>();
Mapper.CreateMap<Namespace1.EventID, Namespace2.EventID>();

That would be why the compiler cannot read the second argument in your ForMember, resulting in the confusion what overload it should choose.

0

The "Missing type map configuration or unsupported mapping" is thrown when automapper cannot find the correct mapping for a specific mapping that you're trying to do.

It could be related to where in the code you call your Mapper.CreateMap function, it needs to be called before any calls to Mapper.Map.

Also, as you have two classes that are identical make sure you are creating the map in the correct order. The first Generic typ argument is the source and the second is the destination. For example, perhaps you need to create this map:

Mapper.CreateMap<Namespace2.EventID, Namespace1.EventID>()

as opposed to this map:

Mapper.CreateMap<Namespace1.EventID, Namespace2.EventID>()

As you stated you should not have to specifiy any ForMember() calls as both classes have the same structure.

2
  • I am converting object Namespace1.EventID(i.e.Source object) to Namesapce2.EventID(i.e.destination object). So the sequence is right in Mapper.CreateMap() Mar 4, 2014 at 12:43
  • Too bad it wasn't that simple =)
    – Robban
    Mar 4, 2014 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.