Setting up Automapper 5.1

In this case, there is a problem with the wiki. I wanted to use Automapper 5.2. I cannot find a simple end for a final example that shows a solid configuration with context. In context, I mean where do you put the configuration files and what is the difference between static and api instance?

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

How do you install this package? I have a model called Client, as shown 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; } } 

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

I am not sure how to install AutoMapper regarding configuration. That is, they talk about the global.asax file, and I use the aspnet core. The file Global.asax is missing.

What do you insert in the Startup.cs file, if anything.

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

Hi

0
asp.net-core automapper
source share
1 answer

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

1. Create a mapping profile class that extends from Profile

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

2. Create an AutoMapper configuration class and add the mapping profile class here.

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

3. Create an extension method, so we can add this to the 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. Call the extension method in the Startup.cs ConfigureServices method

  public void ConfigureServices(IServiceCollection services) { services.AddDbContext<DBContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddMvc(); services.AddAutoMapper(); } 
+7
source share

All Articles