我有这个简单的数据模型:
// Model
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
.... Another values here ....
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
.... Another values here ....
}
// ViewModel
public class PersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
我想的值映射(使用AutoMapper) PersonViewModel
到相应的属性(其中AutoMapper发现,如果该属性应该在根对象或者子对象的内部)。 记住,AutoMapper不应创建既不Person对象,也没有地址 (对象必须手动创建,以填补自动映射之前另一属性),并AutoMapper使用已经存在的对象。 例如:
var addressObj = new Address
{
... Filling some values...
};
var personObj = new Person
{
Address = addressObj;
... Filling some values...
};
mapper.Map(personViewModelObj, personObj); // How to make this work for both Person and Address properties?
我怎样才能像自动映射到两个人物的属性和地址属性的作用?
我要补充的两个映射规则(地址和人),并执行mapper.Map()
两次?