使用忽略属性时,AutoMapper ForMember不起作用

时间:2018-10-05 20:58:55

标签: c# automapper

我正在使用自动映射器来映射两个对象,但是当我调用

Mapper.Map<PropertyDto>(CreatePropertyRequestDto, property)

抛出异常

  

找到未映射的成员。在下面查看类型和成员。添加一个   自定义映射表达式,忽略,添加自定义解析器或修改   源/目标类型对于没有匹配的构造函数,请添加一个无参数   ctor,添加可选参数或映射所有构造函数参数   ================================================== ================================================== =============================== AutoMapper为您创建了此类型映射,但您的类型不能为   使用当前配置映射。 CreatePropertyRequestDto->   PropertyDto(目标成员列表)   PropertyHippo.Properties.Shared.HttpRequestResponse.Dto.CreatePropertyRequestDto   -> PropertyHippo.Properties.Shared.Dto.PropertyDto(目标成员列表)

     

未映射的属性:PropertyId Guid CreateDate UpdateDate   LastEditedBy GuidString

下面是我的配置。

    CreateMap<CreatePropertyRequestDto, PropertyDto>()
        .ForMember(dest => dest.PropertyId, opt => opt.Ignore())
        .ForMember(dest => dest.Guid, opt => opt.Ignore())
        .ForMember(dest => dest.CreateDate, opt => opt.Ignore())
        .ForMember(dest => dest.UpdateDate, opt => opt.Ignore())
        .ForMember(dest => dest.LastEditedBy, opt => opt.Ignore())
        .ForMember(dest => dest.GuidString, opt => opt.Ignore());

我已经搜索了答案,发现了thisthisin the docs,但是我仍然看不到自己在做什么。

我想念什么?

编辑

添加了断点,可以看到该代码块正在被命中。作为测试,我还删除了有问题的属性,可以看到映射按预期工作

1 个答案:

答案 0 :(得分:2)

@Progman感谢您提出创建MCVE的建议。创建MCVE时,我在初始化映射器时遇到问题,并发现了this answer

我的问题是我正在使用静态方法Mapper.Map,并且应该注入一种IMapper(在编写此答案时,我看到我的问题错过了如何设置mapperConfigurations)。

Startup.cs

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.*;
//Implementations of packages

public class NestedPanels extends JPanel {
   private static final String[] BTN_TEXTS = { "Say Hello", "Say You Look Good", "Say Sorry I'm Late" }; //three buttons
   private static final int TITLE_POINTS = 3; //number of objects in text box

   public NestedPanels() { //implemeted class
      JPanel southBtnPanel = new JPanel(new GridLayout(3, 2, 1, 1)); //grid layout of buttons and declaration of panel SoutbtnPanel
      for (String btnText : BTN_TEXTS) { //BTN TEXT button titles linked to string btnText label
         southBtnPanel.add(new JButton(btnText)); //add btnText label
      }
      setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); //layout of buttons "Button text"
      setLayout(new BorderLayout());
      add(Box.createRigidArea(new Dimension(600, 600))); //space size of text box webapp over all
      add(southBtnPanel, BorderLayout.SOUTH);
   }

   private static void createAndShowGui() {//class to show gui
        NestedPanels mainPanel = new NestedPanels(); //mainPanel new class of buttons instantiation
        JFrame frame = new JFrame("Date Sim 1.0");//title of webapp on top
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setVisible(true);
        ImageIcon icon = new ImageIcon("C:/Users/wchri/Pictures/10346538_10203007241845278_2763831867139494749_n.jpg");
        JLabel label = new JLabel(icon);
        mainPanel.add(label);
        frame.setDefaultCloseOperation
        (JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
   }

   public static void main(String[] args) {
        System.out.println("Welcome to Date Sim 1.0 with we1. Are you ready to play? Yes/No?");

        Scanner in = new Scanner(System.in);

        String confirm = in.nextLine();

        if (confirm.equalsIgnoreCase("Yes")) {
            System.out.println("Ok hot stuff... Let's start.");

            NestedPanels mainPanel = new NestedPanels();
        } else {
            System.out.println("Maybe some other time!");

            return;
        }

         SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Handler.cs

public void ConfigureServices(IServiceCollection services)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile(new AutoMapperConfig());
            cfg.AddProfile(new SqlAutoMapperConfig());
        });

        services.AddSingleton(config.CreateMapper());

        services.AddMvc();

        _container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

        _container.Register<IMapper>(() => mapperConfig.CreateMapper(_container.GetInstance));
    }