我遇到了AutoMapper(.NET 4.5上的3.1.1)库的问题很奇怪,我将非常感谢您的帮助。
我有四个对象:
public class UserDetail
{
public int Salary { get; set; }
public Address House { get; set; }
public Address Office { get; set; }
}
public class UserDetailEntity
{
public int Salary { get; set; }
public Guid HouseId { get; set; }
public Guid? OfficeId { get; set; }
public virtual AddressEntity House { get; set; }
public virtual AddressEntity Office { get; set; }
}
public class Address
{
public string Street { get; set; }
}
public class AddressEntity
{
public string Street { get; set; }
}
在这种情况下:
Mapper.CreateMap<Address, AddressEntity>();
Mapper.CreateMap<UserDetail, UserDetailEntity>()
.ForMember("Office", s => s.MapFrom(ud => ud.Office));
UserDetailEntity entity = Mapper.Map<UserDetail, UserDetailEntity>(dto);
Assert.AreEqual(dto.House.Street, entity.House.Street);
Assert.AreEqual(dto.Office.Street, entity.Office.Street);
断言在办公室地址失败我总是得到众议院地址:(
谢谢!
答案 0 :(得分:2)
最后我解决了这个问题,AutoMapper使用GetHashCode来确定两个对象是否相同,并且当我覆盖此方法时,在这种情况下哈希码是相同的。