0
votes

This my Source class

public class Content :IAggregateRoot
    {
        public Guid Id { get; set; }
        public string HeaderImage { get; set; }
        public string AboutText { get; set; }
        public string Address { get; set; }
        public long Phone { get; set; }
        public long Mobile { get; set; }
        public DateTime CreationTime { get; set; }
    }

and this is my Destination class

public class AboutViewModel
    {
        public string AboutText { get; set; }
        public string Address { get; set; }
        public long Phone { get; set; }
        public long Mobile { get; set; }
    }

I just want to ignore extra properties of source and map source to destination with Automapper with this method

public static AboutViewModel ConvertToAboutViewModel(this Content content)
        {
           // Mapper.CreateMap<Content,AboutViewModel>().ForMember(x=>x.AboutText,)
            return Mapper.Map<Content, AboutViewModel>(content);
        }

How can I do that??

1

1 Answers

1
votes

Automapper would ignore those properties by default as they do not exist in the destination class.

Have you actually tried to run your code? if it is not working could you please post details of the error or exception you are getting.

You also need to define your basic map like this:

 Mapper.CreateMap<Content, AboutViewModel>();
 Mapper.CreateMap<AboutViewModel, Content>();

Ensure that the above code is ran before you call ConvertToAboutViewModel