'Automapper profile maps its props to my dto

Hi.
I want just map entity to dto but profile props map to the dto.


Now what can i do for the following problem?

enter image description here

public abstract class DtoProfile<TEntity, TDto> : Profile, IProfile
    {
        public DtoProfile()
        {
            var profile = CreateMap<TEntity, TDto>();

            CustomMapping(profile);

        }
        public virtual void CustomMapping(IMappingExpression<TEntity, TDto> mapping)
        {
        }
    }

public class UserDto : DtoProfile<Users,UserDto>
    {
        public int Id { get; set; }
        public string FullName { get; set; }
        public string NationalCode { get; set; }
        public DateTime CreatedDate { get; set; }
    }


Solution 1:[1]

You're doing this in a wrong way. Mapping Profiles should be for mapping, and not to be inherited from the Dto itself.

You're inherting from Profile in DtoProfile, and these properties are defined in Profile, so they will be there. And as the name of class, it's for mapping profile, and shouldn't be used for Dto.

You should have two classes, one is UserDto where you add all the properties you need to map, and the other is UserDtoProfile which inherits from DtoProfile<User, UserDto> and apply your mappings.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 N0xB0DY