Jeff
Jeff

Reputation: 36603

AutoMapper - mapper not found

I can't seem to get anything working with AutoMapper...

First off, the documentation is wrong or outdated or I'm stupid:

AutoMapperConfiguration.Configure(); // this doesn't exist

Second, I'm creating my maps by calling:

Mapper.CreateMap(myType1, myType2)

where the types are literally exact property maps of one another.

But when I call

Mapper.Map(myInstanceOf1, myType2)

I get a mapper not found error. And if I check the AutoMapper internal _objectMapperCache Dictionary, I can see the internal value for my mapping above is null (hence the exception).

What am I doing wrong?

Upvotes: 1

Views: 8327

Answers (2)

Marnix van Valen
Marnix van Valen

Reputation: 13673

You need to create the AutoMapperConfiguration class yourself. Add a static Configure method and put your configuration code there. For example:

public class AutoMapperConfiguration
{
    public static void Configure()
    {
       Mapper.Initialize( x => x.AddProfile<MyProfile>() );
    }
}

public class MyProfile : Profile
{
    public override string ProfileName
    {
       get { return "MyProfile"; }
    }

    public MyProfile()
    {
    // Configuration here
      CreateMap<Account, AccountViewModel>();
    }
}

Upvotes: 4

escargot agile
escargot agile

Reputation: 22399

Try using the generic syntax, it works for me:

Mapper.CreateMap<A, B>().ForMember(....

b = Mapper.Map<A, B>(a);

Upvotes: 2

Related Questions