简体   繁体   中英

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.

   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. result is a correct resulting object of mapping. But result and userToUpdate are different objects.

The main problem is, why userToUpdate is not updated?

您必须先创建一个地图,否则它将不会更新并返回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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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