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 6072051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:07:02+00:00 2026-05-23T10:07:02+00:00

For some reason, a loop that I use in an AutoMapper map definition is

  • 0

For some reason, a loop that I use in an AutoMapper map definition is iterating more than it should.

The map definition:

    Mapper.CreateMap<AdminGameEditModel, Game>()
        .BeforeMap((s, d) =>
        {
            foreach (var platId in s.PlatformIDs)
            {
                Platform newPlat = _gameRepository.GetPlatform(platId);

                d.Platforms.Add(newPlat);
            }
        })
        .ForMember(dest => dest.BoxArtPath, opt => opt.Ignore())
        .ForMember(dest => dest.IndexImagePath, opt => opt.Ignore())
        .ForMember(dest => dest.Cons, opt => opt.MapFrom(src => String.Join("|", src.Cons)))
        .ForMember(dest => dest.Pros, opt => opt.MapFrom(src => String.Join("|", src.Pros)))
        .ForMember(dest => dest.LastModified, opt => opt.UseValue(DateTime.Now))
        .ForMember(dest => dest.Platforms, opt => opt.Ignore());

The foreach in BeforeMap will, for some reason, iterate over the s.PlatformIDs array multiple times. So, for example, if it contains two values, I’ll get six or more iterations, with the two expected values repeating. The PlatformIDs are not defined as a two dimensional array, and the debugger confirms that the array only contains the values it should, with no repeating sets.

I’m stumped as to what could be causing it.


EDIT: With the loop, I have the following breakpoints –

.BeforeMap((s, d) =>
{
    foreach (var platId in s.PlatformIDs) // breakpoint 1
    {
        Platform newPlat = _gameRepository.GetPlatform(platId); // breakpoint 2

        d.Platforms.Add(newPlat);
    } // breakpoint 3
})

The first pass is normal – breakpoint 1 -> breakpoint 2 -> breakpoint 3. It will then go back to 2, then to 3, which is expected. What’s weird is that it will then jump back to breakpoint 1, and start the process over again.

I’m not sure if there’s a pattern. Two array values results in six passes. One array value results in four passes.


EDIT 2: My hunch was right – BeforeMap is firing more than once.


EDIT 3: The problem persists in AfterMap as well. The method executes more than once per mapping.

  • 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-23T10:07:02+00:00Added an answer on May 23, 2026 at 10:07 am

    Appears to be a legit bug, judging by something similar: http://automapper.codeplex.com/workitem/6604. I’ve written it up as an issue on AutoMapper’s GitHub, and have linked that issue to this question so the devs can see what I was attempting to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, for some reason when I try to use a for loop to initialize
For some reason the Windows command prompt is special in that you have to
Our computer science teacher once said that for some reason it is faster to
I'm trying to loop an exception, but for some reason its not giving me
For some reason I am iterating over elements of a class in an std::set
For some reason my app is using more and more memory as it runs,
I have a real big problem that normally doesn't happen. For some reason on
For some reason I cannot set the loop time because I am generating the
For some reason I'm having trouble getting my program to run a while loop.
For some reason I never see this done. Is there a reason why not?

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.