Nisha_Roy
Nisha_Roy

Reputation: 524

AutoMapper add Profile on assembly load

I want to register all the Mappings in my Business Layer to Data Layer, and Data Layer to Business Layer classes just as my Business Layer assembly loads. Currently I am using a static class to do this task for me:

public static class AutoMapperBootstrapper
{
    public static void InitMappings()
    {
        Mapper.Initialize(a => a.AddProfile<MyProfile>());
    }
}

But each time I make a call with Mapper.Map and the added mappings in the profile, it is still saying Missing Type Mapping Information.

How I should go about fixing this?

Upvotes: 2

Views: 12834

Answers (2)

Todd Miranda
Todd Miranda

Reputation: 29

I don't know if you have gotten an answer to this yet. You are on the right track, but don't use Mapper.Initialize. If you use initialize, you are clearing all of the existing mappings and then adding the ones in the initialize call. Instead just call AddProfile in your static method. Or better yet, just add the profile in your constructor of the BL or DL class.

public static class AutoMapperBootstrapper
{
    public static void AddMappings()
    {
        Mapper.AddProfile<MyProfile>();
    }
}

So in a nutshell, what is happening is you are adding whatever mappings you need for your web tier in the Global.asax or wherever you are adding them. Then the first time your BL or DL is loaded, you are calling initialize which clears any existing mappings. So the next time you go to use a mapping that had already been added, you get the message that it does not exist because it was cleared by the call to initialize.

Upvotes: 0

Prasad Kanaparthi
Prasad Kanaparthi

Reputation: 6563

It seems your Mapper.AddProfile is not calling when the application starts. Try this,

In Global.asax.cs [Application_Start],

protected void Application_Start(object sender, EventArgs e)
{
    Mapper.AddProfile<MyProfile>();
}

And MyProfile looks like below,

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

    protected override void Configure()
    {
        //// BL to DL
        Mapper.CreateMap<BLCLASS, DLCLASS>();

        ////  and DL to BL
        Mapper.CreateMap<DLCLASS, BLCLASS>();
    }
}

Upvotes: 1

Related Questions