0

I am using .Net core with Entity Framework. Below is my code

View Model

public class EmployeeVm
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ContactNo { get; set; }
        public string Email { get; set; }
        public DateTime JoiningDate { get; set; }
        public int BranchId { get; set; }
        public int DepartmentId { get; set; }
    }

POCO Class

public class employee
    {
        [Key]
        public int id { get; set; } 
        public string name { get; set; }
        public string contact_no { get; set; }
        public string email { get; set; }
        public DateTime joining_date { get; set; }
        public int branch_id { get; set; }
        public int department_id { get; set; }      
    }

Automapper configuration from Startup class

public void ConfigureServices(IServiceCollection services)
        {
            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingProfile());
            });

            IMapper mapper = mappingConfig.CreateMapper();
            services.AddSingleton(mapper);
        }

Mapping Profile class

public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            CreateMap<employee, EmployeeVm>();
            CreateMap<EmployeeVm, employee>();
        }
    }

When I am trying to map View Model properties to POCO class properties using below code it is working fine.

//Here I am using constructor injection
private readonly IMapper _mapper;
public EmployeeBl(IMapper mapper)
{
    _mapper = mapper;
}

_mapper.Map<employee>(employeeVm)

But when I am trying to map POCO class (employee) properties to View Module (EmployeeVm) properties then some properties are not mapping as it contains underscore in POCO class

Here is the response of postman

{
    "id": 4,
    "name": "test",
    "contactNo": null,
    "email": "[email protected]",
    "joiningDate": "0001-01-01T00:00:00",
    "branchId": 0,
    "departmentId": 0,
}

From above response I am expecting to map contactNo, joiningDate, branchId and departmentId properties with respective value.

1
  • 1
    This is because your property names are not same. Then you have to configure mapping properties one by one. Commented Jan 22, 2020 at 7:31

3 Answers 3

1

https://docs.automapper.org/en/stable/Configuration.html#naming-conventions

You can set the source and destination naming conventions

var configuration = new MapperConfiguration(cfg => {
  cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
  cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
});

This will map the following properties to each other: property_name -> PropertyName

You can also set this at a per profile level

public class OrganizationProfile : Profile
{
  public OrganizationProfile()
  {
    SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
    DestinationMemberNamingConvention = new PascalCaseNamingConvention();
    //Put your CreateMap... Etc.. here
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, My issue is resolved by putting source and destination naming conventions at profile level. Thanks a lot.
@Yasser Shaikh after adding above naming conventions at profile level, POCO class properties are mapping to View Model's properties, but now View Model properties are not mapping to POCO class properties. I am trying to achieve both way mapping (POCO class to View Model and View Model to POCO class).
1

AutoMapper doesn't map snake_case to PascalCase automatically. You have to configure naming conventions as described here: https://docs.automapper.org/en/v9.0.0/Configuration.html#naming-conventions

Or map the properties one at at a time.

However, assuming your "POCO class" is something you need to store in a database using some persistance framework, a better approach would be to configure your persistance tool with knowledge about the snake_cased column names in the database, and let your C# objects conform to C# naming conventions, which dictate that property names should be PascalCase. This means you can name the properties identically and allow the default AutoMapper config to map your objects.

Comments

0

you can also map the prop which has diff name apart of configuration

Mapper.CreateMap<employee, EmployeeVm>()
    .ForMember(dest => dest.JoiningDate, opt => opt.MapFrom(src => joining_date ));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.