我在映射回 DTO 时遇到问题。
DTO:
public class ServiceEntity : BaseChild
{
public int Id { get; set; }
}
public class BaseChild
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Salary { get; set; }
public string BkName { get; set; }
public int BkPrice { get; set; }
public string BkDescription { get; set; }
}
视图模型:
public class BusinessEntity
{
public ChildBussiness Details { get; set; }
}
public class ChildBussiness
{
public string NameFirst { get; set; }
public string LastName { get; set; }
public Books BookDetails { get; set; }
public string Salary { get; set; }
}
public class Books
{
public string BookName { get; set; }
public int BookPrice { get; set; }
public string BookDescription { get; set; }
}
控制器
对于使用以下代码从 DTO 到 ViewModel 的映射,它工作正常。
public ActionResult Index()
{
ServiceEntity obj = GetData();
Mapper.CreateMap<ServiceEntity, BusinessEntity>()
.ForMember(d => d.Details, o => o.MapFrom(x => new ChildBussiness { NameFirst = x.FirstName, LastName = x.LastName, Salary = x.Salary.ToString(), BookDetails = new Books { BookDescription = x.BkDescription, BookName = x.BkName, BookPrice = x.BkPrice }}));
BusinessEntity objDetails = Mapper.Map<ServiceEntity, BusinessEntity>(obj);
}
虽然转换回来我做不到。下面的代码我试过了。
.
.
.
ServiceEntity objser = new ServiceEntity();
Mapper.CreateMap<BusinessEntity, ServiceEntity>();
Mapper.CreateMap<Books, ServiceEntity>();
objser = Mapper.Map<BusinessEntity, ServiceEntity>(model);
.
.
.
但我没有取得任何成功。例如,我提供了一些属性。我实时可能拥有超过 30 处房产。任何建议将不胜感激...