自动映射器映射覆盖嵌套的子实体属性

时间:2015-09-23 14:30:42

标签: c# .net object nested automapper

我刚刚开始使用Automapper,并且阻止了一个与嵌套子实体有关的特定行为。

在更新父对象的子对象时,Automapper将使用来自Target数据的数据创建的新实例替换目标对象中的子对象,而不是按属性复制属性。为了更好地解释这个问题,我复制了下面的测试代码:

class Program
{
    static void Main(string[] args)
    {

        Mapper.CreateMap<PersonDTO, Person>();
        Mapper.CreateMap<UserLoginDTO, UserLogin>();

        Person pmodel = new Person()
        {
            Id = 10,
            FullName = "XYZ",
            LoginInfo = new UserLogin() { Id = 10, UserName = "xyz" }
        };

        PersonDTO pdto = new PersonDTO()
        {
            Id = 10,
            FullName = "XYZ",
            LoginInfo = new UserLoginDTO() { Id = 10, UserName = "abc" }
        };

        pmodel.Initiated();
        pmodel.LoginInfo.Initiated();
        Console.WriteLine("model => LoginInfo => IsInitiated : {0}", pmodel.LoginInfo.IsInitiated);
        Mapper.Map<PersonDTO, Person>(pdto, pmodel);
        Console.WriteLine("model => LoginInfo => IsInitiated : {0}", pmodel.LoginInfo.IsInitiated);

    }
}

public class PersonDTO
{
    public int Id
    {
        get;
        set;
    }

    public String FullName
    {
        get;
        set;
    }


    public UserLoginDTO LoginInfo { get; set; }
}

public class UserLoginDTO
{

    public int Id
    {
        get;
        set;
    }

    public String UserName
    {
        get;
        set;
    }
}

public class Person : BaseEntity
{
    private int _id;
    private string _name;
    private UserLogin _loginInfo;

    public int Id { 
        get { return _id;  }
        set { _id= value; }
    }

    public String FullName
    {
        get { return _name; }
        set { _name = value; }
    }

    public UserLogin LoginInfo { get { return _loginInfo; } set { _loginInfo = value; } }

}

public class UserLogin : BaseEntity
{
    private int _id;
    private string _userName;

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    public String UserName
    {
        get { return _userName; }
        set { _userName= value; }
    }
}


public abstract class BaseEntity
{
    private bool _isInitiated = false;

    public bool IsInitiated { get { return _isInitiated; } }

    public void Initiated()
    {
        this._isInitiated = true;

    }
}

以上代码的输出是:

在映射之前:model =&gt; LoginInfo =&gt; IsInitiated:是的 映射后:model =&gt; LoginInfo =&gt; IsInitiated:False

我的要求是在映射后保留IsInitiated属性。我在这里错过了什么。

1 个答案:

答案 0 :(得分:0)

我有同样的问题。我最终使用了该映射方法的返回值。

var result = Mapper.Map<PersonDTO, Person>(pdto, pmodel);

并为我不想覆盖的属性添加了显式忽略。

  Mapper.CreateMap<PersonDTO, Person>()
.ForMember(d => d.IsInitiated, op => op.Ignore());