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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:44:08+00:00 2026-06-14T14:44:08+00:00

I’m trying to use nested mappings with AutoMapper, using the instance version of the

  • 0

I’m trying to use nested mappings with AutoMapper, using the instance version of the mapper — but it doesn’t seem to be working.

Here’s two models I’m using:

public class User
{
    [Key]
    public string Email { get; set; }
    public string Hash { get; set; }
    public string Salt { get; set; }
    public string Name { get; set; }

    public virtual ICollection<TaskTime> TaskTimes { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
    public virtual ICollection<HistoricalEstimation> HistoricalEstimations { get; set; }
}

public class TaskTime
{
    public int Id { get; set; }
    public User User { get; set; }
    public Task Task { get; set; }
    public TimeSpan Duration { get; set; }
    public DateTime Date { get; set; }
}

And I’m using this code to map them:

public static class UserViewConfiguration
{
    private static ConfigurationStore configuration;
    public static MappingEngine Engine { get; private set; }

    static UserViewConfiguration()
    {
        configuration = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers());
        Engine = new MappingEngine(configuration);

        configuration.CreateMap<User, UserFull>();
        configuration.CreateMap<TaskTime, UserTaskTime>();
        /* snip... */

        configuration.AssertConfigurationIsValid();
    }
}

onto these viewmodels:

public class UserFull
{
    public string Email { get; set; }
    public string Name { get; set; }

    public virtual ICollection<TaskTime> TaskTimes { get; set; }
}

public class UserTaskTime
{
    public int Id { get; set; }
    public Task Task { get; set; }
    public TimeSpan Duration { get; set; }
    public DateTime Date { get; set; }
}

The problem is that, a User contains a TaskTime, and a TaskTime contains a User. This cycle needs to be present since there are several different ways of getting to each item, depending on which object you originally asked for (which is why I’m using the instance version of AutoMapper). I’m serializing these to send them through an ASP.NET MVC API app, so the cycle is a big problem.

I’ve read this example of using nested mappings with AutoMapper, and from what I can tell I’m doing it right. But with the mappings above, I’m getting a self-referencing loop error on User for the path [0].TaskTimes[0].User.TaskTimes. If I comment out the TaskTimes property of UserFull, I don’t get an error, so I know the User->UserFull mapping is working — but for some reason the TaskTime->UserTaskTime is not working.

What can I do?

Edit: I’m mapping like this:

// GET api/Users
public IEnumerable<UserFull> GetUsers()
{
    //var query = SelectUsers(db.Users.ToList());
    return UserViewConfiguration.Engine.Map<IEnumerable<UserFull>>(db.Users);
}
  • 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-06-14T14:44:09+00:00Added an answer on June 14, 2026 at 2:44 pm

    Assuming that you have a typo, and that UserFull should have a collection of UserTaskTime’s rather than a collection of TaskTime’s, these quick tests work:

    [TestFixture]
    public class MappingTests2
    {
        [Test]
        public void AutoMapper_Configuration_IsValid()
        {
            UserViewConfiguration.Configure();
            Mapper.AssertConfigurationIsValid();
        }
    
        [Test]
        public void AutoMapper_MapsAsExpected()
        {
            UserViewConfiguration.Configure();
            Mapper.AssertConfigurationIsValid();
    
            var user = new User
                {
                    Email = "user1@email.com",
                    Hash = "1234Hash",
                    Name = "user1",
                    Salt = "1234Salt",
                    TaskTimes =
                        new Collection<TaskTime>
                            {
                                new TaskTime
                                    { Date = new DateTime(2012, 11, 01), Duration = new TimeSpan(0, 20, 1), Id = 1 },
                                new TaskTime
                                    { Date = new DateTime(2012, 11, 02), Duration = new TimeSpan(0, 20, 2), Id = 2 }
                            }
                };
    
            foreach (var taskTime in user.TaskTimes)
            {
                taskTime.User = user;
            }
    
            var userView = Mapper.Map<User, UserFull>(user);
    
            Assert.That(userView, Is.Not.Null);
            Assert.That(userView.Email, Is.EqualTo("user1@email.com"));
            Assert.That(userView.Name, Is.EqualTo("user1"));
            Assert.That(userView.TaskTimes, Is.Not.Null);
            Assert.That(userView.TaskTimes.Count, Is.EqualTo(2));
            var tt = userView.TaskTimes.FirstOrDefault(x => x.Id == 1);
            Assert.That(tt, Is.Not.Null);
            Assert.That(tt.Id, Is.EqualTo(1));
            Assert.That(tt.Date, Is.EqualTo(new DateTime(2012, 11, 01)));
            Assert.That(tt.Duration, Is.EqualTo(new TimeSpan(0, 20, 1)));
        }
    }
    

    Note that for the above I converted the mapping back to using the static methods:

    public static void Configure()
    {
        Mapper.CreateMap<User, UserFull>();
        Mapper.CreateMap<TaskTime, UserTaskTime>();
    
        Mapper.AssertConfigurationIsValid();
    }
    

    I’ll take another look if you did intend it to be TaskTime.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.