1

I have tried many options to get Automapper to correctly map Parent / Child relationships.

Model:

//Entity
public class WorkArea
{
  public Guid Id;
  public Name {get;set;}
  public Guid? ParentWorkAreaId {get;set;} //for entity Framework Foreign Key 
  public WorkArea ParentWorkArea {get;set;}
  public ICollection<WorkArea> ChildWorkareas {get;set;}
}

//DTO
public class WorkAreaDto
{
  public Guid Id;
  public Name {get;set;}
  public Guid? ParentWorkAreaId {get;set;} //for entity Framework Foreign Key 
  public WorkAreaDto ParentWorkArea {get;set;}
  public ICollection<WorkAreaDto> ChildWorkareas {get;set;}
}

This mapping causes a Stack Overflow:

Mapper.CreateMap<WorkArea,WorkAreaDto>();

I tried something exactly like this and had the same error

I then created a custom TypeConverter, but not only do I have to write recursive methods for children, but also parents. Just seems like a lot of work do get this to map correctly. Not sure if I am doing something wrong. I am using 2.0

Update: I think my issue is the System.Data.Entity.DynamicProxies generated by Entity Framework.

Community
  • 1
  • 1
DDiVita
  • 4,225
  • 5
  • 63
  • 117
  • I tested your code above and did not receive a StackOverflowException when running the CreateMap call. Are you sure that's where you're seeing the exception? – PatrickSteele Feb 02 '12 at 13:11
  • Is the exception happening when you call Mapper.CreateMap<>() or when you call Mapper.Map<>()? – Davin Tryon Feb 04 '12 at 23:51
  • @PatrickSteele: I meant that the CreatMap<> is what I was using to establish the mapping. I receive the stackoverflow on Mapper.Map<> – DDiVita Feb 06 '12 at 12:42
  • Can you post a complete failing test that exhibits the problem you're having? – PatrickSteele Feb 06 '12 at 14:11

1 Answers1

1

This works perfectly well for me, perhaps you should inspect your data:

        [TestMethod]
        public void TestMethod1()
        {
            Mapper.CreateMap<WorkArea, WorkAreaDto>();

            var source = CreateSource();

            WorkAreaDto destination = new WorkAreaDto();
            Mapper.Map(source, destination);

            Assert.AreEqual(destination.ChildWorkareas.Count, 3);

        }

        private WorkArea CreateSource()
        {
            var id = Guid.NewGuid();

            var result = new WorkArea();
            result.Id = id;
            result.Name = "Name" + id.ToString();
            result.ParentWorkArea = CreateSourceParent(result);
            result.ParentWorkAreaId = result.ParentWorkArea.Id;
            result.ChildWorkareas = CreateSourceChildren(result);

            return result;
        }

        private ICollection<WorkArea> CreateSourceChildren(WorkArea parent)
        {
            var result = new Collection<WorkArea>
                {
                new WorkArea() { Id = Guid.NewGuid(), Name = "Child1", ParentWorkArea = parent, ParentWorkAreaId = parent.Id },
                new WorkArea() { Id = Guid.NewGuid(), Name = "Child2", ParentWorkArea = parent, ParentWorkAreaId = parent.Id },
                new WorkArea() { Id = Guid.NewGuid(), Name = "Child3", ParentWorkArea = parent, ParentWorkAreaId = parent.Id }
                };

            return result;
        }

        private WorkArea CreateSourceParent(WorkArea source)
        {
            var id = Guid.NewGuid();

            var result = new WorkArea();
            result.Id = id;
            result.Name = "Name" + id.ToString();
            result.ChildWorkareas = new Collection<WorkArea>
            {
                source
            };

            return result;

        }
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • while this works, I think the issue is when I try to map a child that has a parent. So when querying for a Work Area in EF let's assume I get the work area by ID and it is a child. That will have a parent and that parent may be a child. – DDiVita Feb 08 '12 at 11:48
  • The test above shows WorkArea with a parent (with the child set) and 3 children. Do you mean that you think the problem occurs when the WorkArea's parent has another parent? – Davin Tryon Feb 08 '12 at 14:33
  • it looks like my issue steams from the DynamicProxies generated by EF. Your example worked great. – DDiVita Feb 08 '12 at 16:48
  • Have you tried to detach the entity WorkArea (context.Detach) before trying to map? Maybe you are inadvertently calling for more data. – Davin Tryon Feb 08 '12 at 21:18
  • if I detach the WorkArea entity completely, there will be nothing to map. – DDiVita Feb 14 '12 at 00:28