Automapper父子映射Ignore和UseDestinationValue C#

时间:2014-07-20 01:58:41

标签: c# .net mapping automapper

我对AutoMapper不熟悉并且我已经处理了这个问题好几个小时了,我希望有人可以帮助我。我创建了以下示例类来显示我的问题:

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; }
}

我有以下单元测试:

[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);
    }
}

我的问题如下:

当我尝试从Child保留创建日期时,我在ChildModel和Child之间进行映射一切正常。 .Ignore()按预期工作(参见测试方法“Retain_CreatedDate_Works”)。

但是,当我尝试从Child保留创建的日期并且我在ParentModel和Parent之间进行映射时,不会保留创建的日期。设置为“1/1/0001 12:00:00 AM”(参见测试方法“Retain_CreatedDate_Doesnt_Works”)。

所以我有两个问题: 1)当我将ParentModel映射到Parent时,我必须做什么,保留父创建日期和所有Child的创建日期;那就是保留目的地之一。

2)我尝试使用UseDestinationValue(),使用以下映射进行不起作用的测试:

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

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

并抛出以下异常:

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

为什么我会收到此错误?

提前感谢您的帮助。

0 个答案:

没有答案