我将嵌套ViewModel映射到Parent ViewModel时出现问题: 这些是我的实体:
public class Patient : DeletableEntity
{
public int? PersonalDataId { get; set; }
public virtual PersonalData PersonalData { get; set; }
}
public class PersonalData : DeletableEntity
{
[StringLength(10)]
public string EGN { get; set; }
[Column(TypeName = "datetime2")]
public DateTime? DateOfBirth { get; set; }
[Range(0, 200)]
public int? Age { get; set; }
public Gender Gender { get; set; }
[Required]
[MaxLength(50)]
public string FirstName { get; set; }
[MaxLength(50)]
public string SecondName { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
...
}
这些是ViewModels:
public class PatientModel : IMapFrom<Patient>, IHaveCustomMappings
{
public int? Id { get; set; }
public PersonalDataModel PersonalDataModel { get; set; }
}
public class PersonalDataModel : IMapFrom<PersonalData>, IHaveCustomMappings
{
[StringLength(10)]
[Display(Name = "PersonalData_EGN", ResourceType = typeof(Resource))]
public string EGN { get; set; }
[Display(Name = "PersonalData_DateOfBirth", ResourceType = typeof(Resource))]
public DateTime DateOfBirth { get; set; }
[Range(0, 200)]
[Display(Name = "PersonalData_Age", ResourceType = typeof(Resource))]
public int? Age { get; set; }
[Display(Name = "PersonalData_Gender", ResourceType = typeof(Resource))]
public Gender Gender { get; set; }
[Required(ErrorMessageResourceName = "PersonalDataModel_FirstNameRequiredErrorMessage", ErrorMessageResourceType = typeof(Resource))]
[MaxLength(50)]
[Display(Name = "PersonalData_FirstName", ResourceType = typeof(Resource))]
public string FirstName { get; set; }
[MaxLength(50)]
[Display(Name = "PersonalData_SecondName", ResourceType = typeof(Resource))]
public string SecondName { get; set; }
[Required(ErrorMessageResourceName = "PersonalDataModel_LastNameRequiredErrorMessage", ErrorMessageResourceType = typeof(Resource))]
[MaxLength(50)]
[Display(Name = "PersonalData_LastName", ResourceType = typeof(Resource))]
public string LastName { get; set; }
...
}
这是PersonalDataModel的配置:
configuration.CreateMap<PersonalDataModel, Address>();
configuration.CreateMap<PersonalDataModel, Telephone>();
configuration.CreateMap<PersonalDataModel, PersonalData>()
.ForMember(p => p.Address, opt => opt.MapFrom(model => model))
.ForMember(p => p.Telephone, opt => opt.MapFrom(model => model))
.ForMember(p => p.MobileTelephone, opt => opt.MapFrom(model => new Telephone { Number = model.MobileTelephoneNumber }))
.ForMember(p => p.Fax, opt => opt.MapFrom(model => new Telephone {Number = model.FaxNumber }))
.ReverseMap();
但我不知道如何为PatientModel编写配置。