How to specify mapping rule when names of properties differ

Never mind, I myself found a solution:

Mapper.CreateMap<Employee, EmployeeDto>()
    .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.Name));

Just to roll the comments above into an updated approach using Automapper 8.1+...

var mapConfig = new MapperConfiguration(
   cfg => cfg.CreateMap<Employee, EmployeeDto>()
      .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.Name))
);

Then you would build the mapper using the mapConfig:

var mapper = mapConfig.CreateMapper();

We can also specify on Class attributes for mapping

From https://docs.automapper.org/en/stable/Conventions.html#attribute-support

Attribute Support

AddMemberConfiguration().AddName<SourceToDestinationNameMapperAttributesMember>(); * Currently is always on

Looks for instances of SourceToDestinationMapperAttribute for Properties/Fields and calls user defined isMatch function to find member matches.

MapToAttribute is one of them which will match the property based on name provided.

public class Foo
{
    [MapTo("SourceOfBar")]
    public int Bar { get; set; }
}

Considering that we have two classes

public class LookupDetailsBO
    {
        public int ID { get; set; }

        public string Description { get; set; }

    }

and the other class is

public class MaterialBO
    {
        [MapTo(nameof(LookupDetailsBO.ID))]
        public int MaterialId { get; set; }

        [MapTo(nameof(LookupDetailsBO.Description))]
        public string MaterialName { get; set; }

        public int LanguageId { get; set; }
    }

In this way you know typically to which property you follow . and you make sure of the naming convention , so if you have changed the propery name in the source . The MapTo() will prompt an error


Comments

  1. Gavin

    • 2015/8/31

    Never mind, I myself found a solution: Mapper.CreateMap<Employee, EmployeeDto>() .ForMember(dest => dest.FullName, opt => opt.

  2. Ty

    • 2021/2/21

    Note: The name of property "Name" of Employeeclass is not the same as that of property "FullName" of EmployeeDtoclass. And here's the code to map the Employeeobject to EmployeeDto: Mapper.CreateMap<Employee, EmployeeDto>(); // code line (***)EmployeeDto dto = Mapper.Map<Employee, EmployeeDto>(employee);

  3. Jaziel

    • 2016/9/10

    How to specify mapping rule when names of properties differ. Solution: Never mind, I myself found a solution: Mapper.CreateMap<Employee, EmployeeDto>() .

  4. Jayce

    • 2017/5/16

    G.K. Stylios, in Joining Textiles, 2013 Abstract: Sewing machine interactions at different speeds have been used to construct qualitative rules mapping fabric properties to optimum sewing machine settings for intelligent sewing machines. the inference procedures of fuzzy logic have been implemented in a neural network to allow for optimisation of output membership functions and, subsequently

  5. Phoenix

    • 2018/7/23

    I am a newbie to the Automapper framework. I have a domain class and a DTO class as follows:public class Employee{ public long Id {get;set;} public string 

  6. Isaias

    • 2018/7/27

    The following example shows how to specify that a property on a CLR type is not mapped to a column in the database. modelBuilder.Entity<Department>().Ignore(t => t.Budget); Mapping a CLR Property to a Specific Column in the Database. The following example maps the Name CLR property to the DepartmentName database column.

  7. Sylas

    • 2017/7/29

    Note: The name of property "Name" of Employee class is not the same as that of property "FullName" of EmployeeDto class. And here's the code to map the Employee 

  8. Noel

    • 2020/11/23

    For other scenarios that require special handling of JSON property names and values, you can implement custom converters. Customize individual property names. To set the name of individual properties, use the [JsonPropertyName] attribute. Here's an example type to serialize and resulting JSON:

  9. Kelly

    • 2018/8/24

    In the following mapping the property baz will only be mapped if it is greater than or equal to 0 in the source object. var configuration = new 

  10. River

    • 2015/4/9

    The full set of process map/flowchart shapes are at the bottom of this guide. Types of Process Maps. Following is a list of different types of process maps along with a brief description and when you can use each. Choose the type of process map that is most suitable for your goal. Basic Flowchart

  11. Easton

    • 2016/10/5

    Although AutoMapper covers quite a few destination member mapping For whatever reason, we want Total to be the sum of the source Value properties.

  12. Aron

    • 2018/2/12

    Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. Each document is a collection of fields, which each have their own data type. When mapping your data, you create a mapping definition, which contains a list of fields that are pertinent to the document. A mapping definition also includes

  13. Rhett

    • 2019/2/6

    We had to map each property of these two different objects. automapper rule it will map to those properties whose names are the same.

  14. Josue

    • 2016/2/21

    If you don't need to define any custom mapping logic for the However, often you won't have total control of property names for one or 

  15. Kylo

    • 2020/8/12

    How do I generate a random int number? 19. Automapper: Mapping a property value of an object to a string. 167. How to specify mapping rule when names of 

  16. Sincere

    • 2016/12/14

    When both the type involved in the mapping contains properties of the complex type then in What happen if the Complex type property names are different?

Comments are closed.

Recent Posts