ScumSprocket
ScumSprocket

Reputation: 440

How to AutoMapper a Source's Member's Property to Destination Property?

In these 3 classes, how do you map SourceString to DestinationString?

public class MySourceClass
{
   public AnotherClass AnotherClass { get; set; }
}

public class AnotherClass 
{
   public string SourceString { get; set; }
}

// [AutoMap(typeof(MySourceClass))] // <--this doesn't seem to help
public class MyDestinationClass
{
   //[SourceMember( nameof(AnotherClass.SourceString) )] <--not a winner
   public string DestinationString { get; set; }  // <-- this should map to AnotherClass.SourceString
}

If there were a way to do this in the CreateMap method, it seems like a better place to keep the logic. Any help is appreciated!

Upvotes: 0

Views: 432

Answers (1)

Yair I
Yair I

Reputation: 1133

On your automapper configuration you need to create a map for the 2 classes, because the properties are with different names you should add specific map.

CreateMap<AnotherClass , MyDestinationClass>()
    .ForMember(x => x.DestinationString, x => x.ResolveUsing(y => y.SourceString));

Upvotes: 1

Related Questions