0

I met today with strange behavior of AutoMapper library. Why string.Format in the second case does not work? Are you able to explain to me why the first case works when the second throwing an exception?


Mapping configuration: (working)

config.CreateMap<Person, PersonView>().
    ForMember(dest => dest.FullName, opt => opt.
       MapFrom(src => src.Data.Code + " - " + src.Number));

Mapping configuration: (not working)

config.CreateMap<Person, PersonView>().
    ForMember(dest => dest.FullName, opt => opt.
        MapFrom(src => string.Format("{0} - {1}", src.Data.Code, src.Number)));

Projection:

using (var ctx = new MyDbContext())
{
    return ctx.Persons.Include(i => i.Data).Project().To<PersonView>().ToList();
}

Exception:

enter image description here

3

1 Answer 1

4

It's not really anything to do with AutoMapper.

You're effectively asking Entity Framework to translate an expression that calls a method (string.Format in this case) into SQL. It can't do that.

The first works because it's a simple string concatenation, which is more readily translated.

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

Comments

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.