我在做第三級映射時遇到了問題。這些類如下所示:如何映射嵌套類型
public class Company {
public Contact MainContact { get; private set; }
}
public class Contact {
public PersonsName ContactName { get; private set; }
}
public class PersonsName {
public String FirstName { get; private set; }
public String LastName { get; private set; }
}
public class CreateCompanyViewModel {
[Required]
[MaxLength(25, ErrorMessage = "Only 25 characters are allowed.")]
[Display(Name = "First Name")]
public String FirstName { get; set; }
[Required]
[MaxLength(25, ErrorMessage = "Only 25 characters are allowed.")]
[Display(Name = "Last Name")]
public String LastName { get; set; }
}
我嘗試沒有成功如下:
Mapper.CreateMap<CreateCompanyViewModel, PersonsName>()
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName));
Mapper.CreateMap<Contact, PersonsName>()
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.ContactName.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.ContactName.LastName));
Mapper.CreateMap<CreateCompanyViewModel, Company>()
.ForMember(dest => dest.MainContact.ContactName.FirstName, opt => opt.MapFrom(src => src.FirstName))
.ForMember(dest => dest.MainContact.ContactName.FirstName, opt => opt.MapFrom(src => src.LastName));
該錯誤消息我得到的是:
表達「DEST => dest.MainContact.ContactName .FirstName'必須解析爲頂級成員,而不是任何子對象的屬性。改爲在子類型或AfterMap選項上使用自定義解析器。 參數名:lambdaExpression
[Automapper map into nested class]的可能重複(http://stackoverflow.com/questions/7924185/automapper-map-into-nested-class) – 2014-12-03 14:03:43
標題就是誤導,它實際上是一個關於映射到多個類的問題,全部在頂層。 – 2014-12-06 10:07:44
你能顯示'CreateCompanyViewModel'嗎? – 2014-12-06 19:50:21