1

I am failry new to AutoMapper and I have been dealing with this issue for hours and I hope someone can help me. I created the following sample classes to show my problem:

public class Parent
{
    public string Name { get; set; }
    public DateTime CreatedDate { get; set; }
    public List<Child> Children { get; set; }
}

public class Child
{
    public int Age { get; set; }
    public DateTime Created { get; set; }
}

public class ParentModel
{
    public string Name { get; set; }
    public List<ChildModel> Children { get; set; }
}

public class ChildModel
{
    public int Age { get; set; }
}

I have the following unit tests:

[TestMethod]
public void Retain_CreatedDate_Doesnt_Works()
{
    Mapper.CreateMap<ParentModel, Parent>()
        .ForMember(m => m.CreatedDate, o => o.Ignore());

    Mapper.CreateMap<ChildModel, Child>()
        .ForMember(m => m.Created, o => o.Ignore());

    var created = DateTime.Now.AddDays(-1);
    var parent = new Parent
    {
        CreatedDate = created,
        Children = new List<Child>
                {
                    new Child {Age = 1, Created = created},
                    new Child {Age = 2, Created = created},
                    new Child {Age = 3, Created = created}
                }
    };

    var childModel = new ChildModel { Age = 4 };
    var parentModel = new ParentModel
    {
        Children = new List<ChildModel>
                {
                    childModel,
                    childModel,
                    childModel
                }
    };


    Mapper.Map<ParentModel, Parent>(parentModel, parent);

    Assert.AreEqual(created, parent.CreatedDate);
    foreach (var child in parent.Children)
    {
        Assert.AreEqual(child.Age, 4);
        Assert.AreEqual(created, child.Created); // Fails. It is set to a default date
    }
}

[TestMethod]
public void Retain_CreatedDate_Works()
{
    Mapper.CreateMap<ParentModel, Parent>()
        .ForMember(m => m.CreatedDate, o => o.Ignore());

    Mapper.CreateMap<ChildModel, Child>()
        .ForMember(m => m.Created, o => o.Ignore());

    var created = DateTime.Now.AddDays(-1);
    var children = new List<Child>
        {
            new Child {Age = 1, Created = created},
            new Child {Age = 2, Created = created},
            new Child {Age = 3, Created = created}
        };

    var childModel = new ChildModel { Age = 4 };

    foreach (var child in children)
    {
        Mapper.Map<ChildModel, Child>(childModel, child);
        Assert.AreEqual(child.Age, 4);
        Assert.AreEqual(created, child.Created);
    }
}

My problem is as follows:

When I try to retain the created date from Child and I do the mapping between ChildModel and Child everything works fine. The .Ignore() works as I expected it (see test method "Retain_CreatedDate_Works").

However, when I try to retain the created date from Child and I do the mapping between ParentModel and Parent, the created date is not preserved. It is set to "1/1/0001 12:00:00 AM" (see test method "Retain_CreatedDate_Doesnt_Works").

So I have 2 questions: 1) What do I have to do so that when I map ParentModel to Parent, the Created date of parent and the created date of all Child are preserved; that is to retain the one from the destination.

2) I tried to use UseDestinationValue() by using the following mapping for the test that doesn't work:

Mapper.CreateMap<ParentModel, Parent>()
    .ForMember(m => m.CreatedDate, o => o.Ignore());

Mapper.CreateMap<ChildModel, Child>()
    .ForMember(m => m.Created, o => o.UseDestinationValue());

and that throws the following exception:

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
ChildModel -> DateTime
ChildModel -> System.DateTime

Destination path:
Parent.Children.Children.Children0[0].Created.Created

Source value:
ChildModel

Why do I get this error?

Thanks in advance for the help.

Gabriel Lopez
  • 159
  • 3
  • 12

0 Answers0