设置Automapper 5.1 [英] Setting up Automapper 5.1

查看:81
本文介绍了设置Automapper 5.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我在关注Wiki时遇到了麻烦.我想使用Automapper 5.2.我找不到一个简单的结尾示例,该示例显示了具有上下文的可靠配置.通过上下文,我的意思是您将配置文件放在哪里,静态和实例api之间有什么区别?

I am having trouble following the wiki in this instance. I wanted to use Automapper 5.2. I cannot find a simple end for end example that shows a solid configuration with context. By context I mean where do you put the config files and whats the difference between static and instance api?

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

I checked out the DNRTV download but it deals with the 1.0 version.

您如何设置此软件包?我有一个名为Client的模型,如下所示.

How do you set this package up? I have a model called Client as below.

    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; }
}

和ClientViewModel一样:

and a ClientViewModel as so:

    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; }
}

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

I am unsure how to set AutoMapper up with regard to configuration. That is, they talk about a global.asax file and I am using aspnet core.. there is no Global.asax file..

您在Startup.cs文件中放了什么(如果有).

What do you put in the Startup.cs file if anything.

鉴于以上两个文件,我需要做什么以将Automapper与它们一起使用?

Given these two files above what do I need to do to use Automapper with them?

致谢

推荐答案

以下是在asp.net核心mvc中配置自动映射器的步骤.

Here is the steps to configure the automapper in asp.net core mvc.

1..创建从Profile

 public class ClientMappingProfile : Profile
 {
     public ClientMappingProfile ()
     {
         CreateMap<Client, ClientViewModel>().ReverseMap();
     }
 }

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

2. Create the AutoMapper Configuration Class and add your mapping profile class here.

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

3..因此,我们可以将其添加到Startup.cs ConfigureServices方法

3. Create extension method so, we can add this to Startup.cs ConfigureServices method

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());
    }
} 

4.Startup.cs ConfigureServices方法

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

     services.AddAutoMapper();
}

这篇关于设置Automapper 5.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆