0
votes

Below is the Automapping profile. An error is thrown on the destination Id being of the wrong input format.

The destination Id property is not even being mapped to.

public class AspGetUserLoginReturnModelToSRSDLUserLoginModel : Profile
{
    public AspGetUserLoginReturnModelToSRSDLUserLoginModel()
    {
        CreateMap<asp_GetUserLoginReturnModel, SRSDLUserLoginModel>()
            .ForMember(dest => dest.UserPassword, opt => opt.MapFrom(src => src.PasswordHash))
            .ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.Id)              
            ;
    }
}

Now the destination class SRSDLUserLoginModel is derived from a base class that has a public accessor named Id which is type of nullable long.

The source Id property used above is of type string.

As the code shown above the source Id is mapped to the destination UserId property.

But Automapper is mapping the source Id to the destination's based classes Id property.

WHY IS THIS HAPPENING?

The code is straight forward and there is nothing new trying to be accomplished. It Automapper like most everything other packages that I have work with?

In the full/classic .NET framework, the packages worked fine, but in .NET Core those packages just suck?

// source class
public partial class asp_GetUserLoginReturnModel
{
    public string Id { get; set; }
}

// destination classes
public class SRSDLUserLoginModel: SRSDLModel, ISRSDLUserLoginModel
{  
    public string UserId { get; set; }
    public string UserPassword { get; set; }
    //  [...]
}

public class SRSDLModel : ISRSDLModel
{  
    public long? Id { get; set; }
    // [...]
}
1
A repro would help. Make a gist that we can execute and see fail. - Lucian Bargaoanu

1 Answers

1
votes

AutoMapper will map properties with the same name by default so you don't have to write them all the time. In this case it tries to set the SRSDLModel.Id property with the value from the asp_GetUserLoginReturnModel.Id property. As you already noticed, that doesn't work because of the mismatch between long? and string.

To fix this, use the .Ignore() method in your mapping to specify that this specific property should no be set during mapping. The configuration might look like this:

.ForMember(dest => dest.Id, i => i.Ignore());