Automapper v8 is not ignoring navigation properties of EF (Entity Framework) class

asp.net-core automapper entity-framework-6

Question

Below is the code for Automapper profile:

public class MyMappingProfile: Profile
{
    public MyMappingProfile()
    {
        Mapper.Initialize(cfg => {
            cfg.CreateMap<MyModelClass, MyEntityClass>()
                .ForMember(dest => dest.NavigationProp1, opt => opt.Ignore())
                .ForMember(dest => dest.NavigationProp2, opt => opt.Ignore());
        });
    }
}

Below is the code for creating a new entity object (FYI, I am using this code in .NET Core project, so _mapper is the :

var newEntity = _mapper.Map<MyModelClass, MyEntityClass>(model);

But the above code line produce the below error:

Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters

AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. MyModelClass -> MyEntityClass (Destination member list) MyModelClass -> MyEntityClass (Destination member list)

Unmapped properties:

NavigationProp1

NavigationProp2

Any idea why Automapper is still complaining for unmapped members even after they are ignored?

Thanks.

1
1
3/13/2019 8:48:44 PM

Popular Answer

I finally got it resolved by changing the mapping profile class as below:

public class MyMappingProfile: Profile
{
    public MyMappingProfile()
    {
        CreateMap<MyModelClass, MyEntityClass>()
            .ForMember(dest => dest.NavigationProp1, opt => opt.Ignore())
            .ForMember(dest => dest.NavigationProp2, opt => opt.Ignore());
    }
}

Cheers.

2
3/27/2019 7:56:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow