简体   繁体   English

AutoMapper不会将源映射到目标

[英]AutoMapper doesn't map source to a destination

I don't know why, at some point AutoMapper doesn't map source to a destination object. 我不知道为什么,在某些时候AutoMapper不会将源映射到目标对象。

   var result = Mapper.Map<User, User>(userToImport, userToUpdate);
   var areEquals = result == userToUpdate; //FALSE !!! Why?
   var areEquals2 = result.Equals(userToUpdate); //FALSE !!! Why?

userToUpdate is not updated with new values from userToImport. userToUpdate不会使用userToImport中的新值更新。 result is a correct resulting object of mapping. result是正确的映射结果对象。 But result and userToUpdate are different objects. 但是resultuserToUpdate是不同的对象。

The main problem is, why userToUpdate is not updated? 主要问题是,为什么userToUpdate没有更新?

您必须先创建一个地图,否则它将不会更新并返回destination参数的对象。

 Mapper.CreateMap<User, User>();

You need to do something like this: 您需要执行以下操作:

public class User
{
    public int Property1 { get; set; }
    public int Property2 { get; set; }

    public override bool Equals(object obj)
    {
        if (!(obj is User))
            return false;
        else
        {
            Usero = obj as User;
            return o.Property1 == this.Property1 && o.Property2 == this.Property2;
        }

    }
}

After that you can do obj1.Equals(obj2); 之后,您可以执行obj1.Equals(obj2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM