AutoMapper - 将源对象映射为目标对象中的 属性

AutoMapper - Map source object as property in the destination object

我有这两个class `

//source 
public class User
{
    [Key]
    public int UserId { get; set; }
    public string Name { get; set; }
    public int Phonee{ get; set; }
    public string Email { get; set; }
    
    public UserEducation userEducation { get; set; }
}

// destination 
public class UserEducation
{
    [Key]
    public int UserEducationId { get; set; }
    public string University { get; set; }
    public int  YearOfGraduation { get; set; }

    public int UserId { get; set; }
    public User User { get; set; }
}

我想做的是尝试将源对象映射到目标对象作为其属性之一

userEducation = _mapper.Map<UserEducation>(User);
CreateMap<User.UserEduation>().FromMember(dir => dir.User, ???);

你可以参考这个例子。它可以很好地将 User 映射到 UserEducation

public class CustomMapper : Profile
{
    public CustomMapper()
    {
        CreateMap<User, UserEducation>()
            .ForMember(dir => dir.User, x=>x.MapFrom(t=> new User
            {
                UserId=t.UserId,
                Name=t.Name,
                Email=t.Email,
                Phonee=t.Phonee 
            }));
    }
}