Im在映射回DTO時遇到問題。當從ViewModel映射回DTO時發生AutoMapper問題
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映射要使用下面的代碼視圖模型即時通訊,並且其工作的罰款。
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);
}
雖然轉換回im無法做到。下面的代碼我試過了。
.
.
.
ServiceEntity objser = new ServiceEntity();
Mapper.CreateMap<BusinessEntity, ServiceEntity>();
Mapper.CreateMap<Books, ServiceEntity>();
objser = Mapper.Map<BusinessEntity, ServiceEntity>(model);
.
.
.
但我沒有得到任何成功。舉個例子,我提供了幾個屬性。我實時我可能有超過30個財產。 任何建議,將不勝感激......
你有任何錯誤或只是空的屬性? –