设置Automapper 5.1

si2*_*030 0 automapper asp.net-core automapper-5

在这种情况下,我无法关注wiki.我想使用Automapper 5.2.我找不到一个结尾示例的简单结尾,它显示了具有上下文的可靠配置.根据上下文,我的意思是你在哪里放置配置文件,以及静态和实例api之间的区别?

我查看了DNRTV下载,但它处理的是1.0版本.

你如何设置这个包?我有一个名为Client的模型,如下所示.

    public class Client : IEntityBase
{
    public Client()
    {
        Jobs = new List<Job>();
    }
    public int Id { get; set; }
    public int ClientNo { get; set; }
    public bool Company { get; set; }
    public string CompanyName { get; set; }
    public string ClientFirstName { get; set; }
    public DateTime DeActivated { get; set; }
    public bool Activity { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateUpdated { get; set; }

    public int? StateId { get; set; }
    public State State { get; set; }

    public int CreatorId { get; set; }
    public User Creator { get; set; }

    public ICollection<Job> Jobs { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和ClientViewModel如下:

    public class ClientViewModel
{
    public int Id { get; set; }
    public int ClientNo { get; set; }
    public bool Company { get; set; }
    public string CompanyName { get; set; }
    public string ClientFirstName { get; set; }
    public DateTime DeActivated { get; set; }
    public bool Activity { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateUpdated { get; set; }
    public int? StateId { get; set; }
    public int CreatorId { get; set; }
    public int[] Jobs { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我不确定如何在配置方面设置AutoMapper.也就是说,他们谈论一个global.asax文件,我正在使用aspnet核心..没有Global.asax文件..

如果有的话,你把什么放在Startup.cs文件中.

鉴于以上这两个文件,我需要做什么才能使用Automapper?

问候

Ahm*_*mar 7

以下是在asp.net core mvc中配置automapper的步骤.

1.创建从中扩展的映射配置文件类Profile

 public class ClientMappingProfile : Profile
 {
     public ClientMappingProfile ()
     {
         CreateMap<Client, ClientViewModel>().ReverseMap();
     }
 }
Run Code Online (Sandbox Code Playgroud)

2.创建AutoMapper配置类并在此处添加映射配置文件类.

public class AutoMapperConfiguration
{
   public MapperConfiguration Configure()
   {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile<ClientMappingProfile>();
        });
        return config;
    }
}
Run Code Online (Sandbox Code Playgroud)

3.创建扩展方法,我们可以将其添加到Startup.cs ConfigureServices方法中

public static class CustomMvcServiceCollectionExtensions
{
   public static void AddAutoMapper(this IServiceCollection services)
   {
       if (services == null)
       {
           throw new ArgumentNullException(nameof(services));
       }
       var config = new AutoMapperConfiguration().Configure();
       services.AddSingleton<IMapper>(sp => config.CreateMapper());
    }
} 
Run Code Online (Sandbox Code Playgroud)

4.在方法中调用扩展Startup.cs ConfigureServices方法

 public void ConfigureServices(IServiceCollection services)
 {
     services.AddDbContext<DBContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
     services.AddMvc();

     services.AddAutoMapper();
}
Run Code Online (Sandbox Code Playgroud)