Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6047351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:20:30+00:00 2026-05-23T07:20:30+00:00

I have a problem with Automapper when I try use custom resolver which uses

  • 0

I have a problem with Automapper when I try use custom resolver which uses dependency injection.

I have the following model:

public class User : Entity
{
    public virtual string Name { get; set; }
    public virtual Country Country { get; set; }
}

public class Country : Entity
{
    public virtual string Name { get; set; }
}

and the following view model:

public class RegistrationViewModel
{
    [Required]        
    public string Name { get; set; }

    public int CountryId { get; set; }

    public IEnumerable<Country> Countries { get; set; }
}

in order to map I use the following code:

Mapper.Map(registrationViewModel, user);

earlier I register the following:

Mapper.Reset();
container = new WindsorContainer();
container.AddFacility<FactorySupportFacility>();
container.Register(Component.For<ISession>().
                               UsingFactoryMethod(() => NHibernateSessionFactory.RetrieveSession()).
                               LifeStyle.Is(LifestyleType.Transient));
container.Register(Component.For(typeof(LoadingEntityResolver<>)).ImplementedBy(typeof(LoadingEntityResolver<>)).LifeStyle.Transient);
 Mapper.Initialize(x =>
      {
           x.AddProfile<BasicProfile>();
           x.ConstructServicesUsing(container.Resolve);
      });

My BasicProfile is the following:

public class BasicProfile : Profile
{
    public const string VIEW_MODEL = "MyBasicProfile";

    public override string ProfileName
    {
        get { return VIEW_MODEL; }
    }

    protected override void Configure()
    {
        CreateMaps();
    }

    private void CreateMaps()
    {
        CreateMap<RegistrationViewModel, User>()
           .ForMember(dest => dest.Country, _ => _.ResolveUsing<LoadingEntityResolver<Country>>().FromMember(src => src.CountryId))
           );

    }
}

The custom resolver is done in the following way:

public class LoadingEntityResolver<TEntity> : ValueResolver<int, TEntity>
    where TEntity: Entity
{
    private readonly ISession _session;

    public LoadingEntityResolver(ISession session)
    {
        _session = session;
    }

   protected override TEntity ResolveCore(int source)
   {
       return _session.Load<TEntity>(source);
   }

}

When the mapping code is being run I get the following exception:

AutoMapper.AutoMapperMappingException : Trying to map ViewModels.RegistrationViewModel to Models.User.
Using mapping configuration for ViewModels.RegistrationViewModel to Models.User
Exception of type ‘AutoMapper.AutoMapperMappingException’ was thrown.
—-> AutoMapper.AutoMapperMappingException : Trying to map ViewModels.RegistrationViewModel to LModels.Country.
Using mapping configuration for ViewModels.RegistrationViewModel to Models.User
Destination property: Country
Exception of type ‘AutoMapper.AutoMapperMappingException’ was thrown.
—-> System.ArgumentException : Type ‘Mapping.LoadingEntityResolver`1[Models.Country]’ does not have a default constructor

I have no idea what may be wrong. It is probably something with constructing the resolver. When I try the following there is no problem:

var resolver = container.Resolve<LoadingEntityResolver<Country>>();

Assert.IsInstanceOf<LoadingEntityResolver<Country>>(resolver);

I would be greatfull for any help.

Best regards
Lukasz

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:20:30+00:00Added an answer on May 23, 2026 at 7:20 am

    You have some pretty hefty DI stuff going on here 🙂 I would avoid having AutoMapper resolve entities from a database or whatever. Makes the code difficult to understand and following objects lifetime could become a nightmare.

    Anyway, to fix your problem simply swap the order from (wrong):

    Mapper.Initialize(x =>
    {
        x.AddProfile<BasicProfile>();
        x.ConstructServicesUsing(container.Resolve);
    });
    

    to (correct):

    Mapper.Initialize(x =>
    {
        x.ConstructServicesUsing(container.Resolve);
        x.AddProfile<BasicProfile>();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a little .Net web application which uses the Amazon
I am using AutoMapper + EF (Entities => POCO) for the following class: public
I have problem with databinding in the Win.Forms DataGridView control. Example: public class A
I have problem when I try insert some data to Informix TEXT column via
I have a problem in my project with the .designer which as everyone know
I have a business model called Customer which has many required properties (via DataAnnotations)
I'm new with AutoMapper and have a problem I'm trying to solve. If I
I'm trying out Entity Framework Code first CTP4. Suppose I have: public class Parent
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.