AutoMapper,如果不存在则不要覆盖现有值
AutoMapper, Don't Overwrite Existing Value if Not Present
我有两个相似但不完全相同的模型
public class ResponseModel
{
public int? AccountId { get; set; }
public string AccountNumber { get; set; }
public string AccountLegalname { get; set; }
public string Link { get; set; }
}
和
public class Information
{
public int? IdentityId { get; set; }
public int? AccountId { get; set; }
public string AccountNumber { get; set; }
public string AccountLegalName { get; set; }
}
我正在尝试像这样结合这两个模型
var test1 = new Information(){
IdentityId = 1234
};
var test2 = new ResponseModel()
{
AccountId = 123214,
AccountLegalname = "test",
AccountNumber = "9239235",
Link = "link"
};
test1 = _mapper.Map<ResponseModel, Information>(test2);
我想要的是在 test1 中结合两个模型值来填充 Information
.
的一个完整实例
但实际发生的是来自 test2 的所有信息都被插入到 test1 并且 test1.IdentityId = null
我试过了,
this.CreateMap<ResponseModel, Information>()
.ForAllMembers(o => o.Condition((source, destination, member) => member != null));
但运气不好。
我怎样才能使 test2 不覆盖存在于 int test1 中但不存在于 test2 中的数据?
如果我没记错的话,您可以将目的地作为参数传递,以通过重载调用该特定功能:
test1 = _mapper.Map(test2, test1 );
我有两个相似但不完全相同的模型
public class ResponseModel
{
public int? AccountId { get; set; }
public string AccountNumber { get; set; }
public string AccountLegalname { get; set; }
public string Link { get; set; }
}
和
public class Information
{
public int? IdentityId { get; set; }
public int? AccountId { get; set; }
public string AccountNumber { get; set; }
public string AccountLegalName { get; set; }
}
我正在尝试像这样结合这两个模型
var test1 = new Information(){
IdentityId = 1234
};
var test2 = new ResponseModel()
{
AccountId = 123214,
AccountLegalname = "test",
AccountNumber = "9239235",
Link = "link"
};
test1 = _mapper.Map<ResponseModel, Information>(test2);
我想要的是在 test1 中结合两个模型值来填充 Information
.
但实际发生的是来自 test2 的所有信息都被插入到 test1 并且 test1.IdentityId = null
我试过了,
this.CreateMap<ResponseModel, Information>()
.ForAllMembers(o => o.Condition((source, destination, member) => member != null));
但运气不好。
我怎样才能使 test2 不覆盖存在于 int test1 中但不存在于 test2 中的数据?
如果我没记错的话,您可以将目的地作为参数传递,以通过重载调用该特定功能:
test1 = _mapper.Map(test2, test1 );