Automapper嵌套映射3个级别

时间:2017-05-19 10:56:41

标签: c# entity-framework-6 automapper

我有以下问题,我需要访问salesheader的销售线项目,当我尝试访问实体通过延迟加载正常工作,但我尝试使用Automapper 6映射 无法访问销售标题中的项目

感谢

public class SalesHeader
{
    public int DocumentNo { get; set; }
    public virtual ICollection<PostedSalesLine> SalesLines { get; set; }
}
public class SalesLine
{
    public int LineNo { get; set; }
    public int DocumentNo { get; set; }
    public int ItemId { get; set; }
    public virtual Item Item { get; set; }
    public int Quantity { get; set; }
    public decimal Amount { get; set; }
}
public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal UnitCost { get; set; }
    public decimal UnitPrice { get; set; }
}
                var result = unitOfWork.SalesHeader.GetById(documenNo);
                Mapper.Initialize(cfg => cfg.CreateMap<SalesHeader, SalesHeaderDTO>()

                return Mapper.Map<SalesHeaderDTO>(result);

1 个答案:

答案 0 :(得分:0)

完成! 不要使用延迟加载,它会造成一堆代理

public static JournalArticle addJournalArticle(
        long userId, long groupId, String title, String contentEn)
        throws Exception {

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setScopeGroupId(groupId);
    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
    Map<Locale, String> titleMap = new HashMap<Locale, String>();
    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();

    titleMap.put(Locale.US, title);
    descriptionMap.put(Locale.US, title);


    try {
        JournalArticleLocalServiceUtil.deleteArticle(groupId, title, serviceContext);
    } catch (Exception ex) {
        System.out.println("Ignoring " + ex.getMessage());
    }

    String xmlContent = "<?xml version='1.0' encoding='UTF-8'?>" +
            "<root default-locale=\"en_US\" available-locales=\"en_US\">" +
                "<static-content language-id=\"en_US\">" +
                    "<![CDATA[" + contentEn + "]]>" +
                "</static-content>" +
            "</root>";

    JournalArticle article = JournalArticleLocalServiceUtil.addArticle(
            userId, groupId, 0, 
            0, 0, title, true, 
            JournalArticleConstants.VERSION_DEFAULT, titleMap, 
            descriptionMap, xmlContent, 
            "general", null, null, null, 
            1, 1, 2014, 0, 0,
            0, 0, 0, 0, 0, true, 
            0, 0, 0, 0, 0, true, 
            true, false, null, null, 
            null, null, serviceContext);

    return article;
}