Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not all members are being mapped #1417

Closed
cryo75 opened this issue Jun 30, 2016 · 18 comments
Closed

Not all members are being mapped #1417

cryo75 opened this issue Jun 30, 2016 · 18 comments
Labels
Milestone

Comments

@cryo75
Copy link

cryo75 commented Jun 30, 2016

After upgrading to 5.0.0 I have one model class (I've only tested this class so far) that has 4 members more than its dto class. Some members are other classes and some can be null or empty (if value types).

On mapping the model to the dto, only a couple of members are mapped. The remaining are all set to null.

On downgrading to 4.2.1, the mapping is successful. I've tried creating a gist for a repro but the gist works as expected with 5.0.0. However the test classes where not that complex as in my actual project.

As I cannot reproduce the error, would it be better to add Automapper into my project and debug the mapping?

@jbogard
Copy link
Member

jbogard commented Jun 30, 2016

Yeah, I don't see any other choice.

One thing to look at is the TypeMap's MapExpression in debug mode. This is the map "plan" that will execute at runtime (you don't need to add the code directly).

@jbogard
Copy link
Member

jbogard commented Jun 30, 2016

Also, use the AutoMapper.Net4 project if you're adding the code directly.

@TylerCarlson1
Copy link
Member

You should be able to see the MapExpression without having to add AutoMapper. You should be able to open up IMapper or Mapper.Instance then go to Configuration._typeMapCache and find the type map in question and see it's expression code.

Also you might want ReadableExpressions.Visualizers so you can actually read the expression generated.

@jbogard
Copy link
Member

jbogard commented Jun 30, 2016

Or Configuration.FindTypeMapFor.

And THERES that visualizer I was looking for lol

@cryo75
Copy link
Author

cryo75 commented Jul 1, 2016

Ok. Done that and we're approaching the culprit. The MapExpression for this particular object has only 4 out of 16 members mapped. The destintation (dto) has 16 members in total. The source (model) has 20.

The MapExpression looks like this:

(src, dest, ctxt) =>
{
    if (src == null)
    {
        null;
    }
    else
    {
        var dest = dest ?? new Address();
        try
        {
            dest.ZipCode = ctxt.Map(src.ZipCode, null);
        }
        catch (Exception ex)
        {
            throw new AutoMapperMappingException("Error mapping types.", ex, AutoMapper.TypePair, AutoMapper.TypeMap, AutoMapper.PropertyMap);
        }

        try
        {
            dest.HouseNumber = src.HouseNumber;
        }
        catch (Exception ex)
        {
            throw new AutoMapperMappingException("Error mapping types.", ex, AutoMapper.TypePair, AutoMapper.TypeMap, AutoMapper.PropertyMap);
        }

        try
        {
            dest.Id = src.Id;
        }
        catch (Exception ex)
        {
            throw new AutoMapperMappingException("Error mapping types.", ex, AutoMapper.TypePair, AutoMapper.TypeMap, AutoMapper.PropertyMap);
        }

        return dest;
    }
}

The configuration is simply:

cfg.CreateMap<AddressModel, AddressDto>(MemberList.Destination).ReverseMap();

So obviously the next question is:
Why does the mapexpression has so few members to map?

(On debuggind CreateMap, I can see that the mapping expression being created has the correct amount of DeclaredMembers for DestinationType and SourceType).

So somwhere else down the line (whilst building the expression?) members are ignored.

@jbogard
Copy link
Member

jbogard commented Jul 1, 2016

Hmmm and the source/dest types match? How about the reverse map?

On Thursday, June 30, 2016, cryo75 notifications@github.com wrote:

Ok. Done that and we're approaching the culprit. The MapExpression for this particular object has only 4 out of 16 members mapped. The destintation (dto) has 16 members in total. The source (model) has 20.

The MapExpression looks like this:

(src, dest, ctxt) =>
{
if (src == null)
{
null;
}
else
{
var dest = dest ?? new Address();
try
{
dest.ZipCode = ctxt.Map(src.ZipCode, null);
}
catch (Exception ex)
{
throw new AutoMapperMappingException("Error mapping types.", ex, AutoMapper.TypePair, AutoMapper.TypeMap, AutoMapper.PropertyMap);
}

    try
    {
        dest.HouseNumber = src.HouseNumber;
    }
    catch (Exception ex)
    {
        throw new AutoMapperMappingException("Error mapping types.", ex, AutoMapper.TypePair, AutoMapper.TypeMap, AutoMapper.PropertyMap);
    }

    try
    {
        dest.Id = src.Id;
    }
    catch (Exception ex)
    {
        throw new AutoMapperMappingException("Error mapping types.", ex, AutoMapper.TypePair, AutoMapper.TypeMap, AutoMapper.PropertyMap);
    }

    return dest;
}

}

The configuration is simply:

cfg.CreateMap<AddressModel,
AddressDto>(MemberList.Destination).ReverseMap();

So obviously the next question is:
Why does the map has so little members to map?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#1417 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAGYMssKpSVy9A1jtOd26Qs8n6yltH-Cks5qRG0wgaJpZM4JCaTM
.

@TylerCarlson1
Copy link
Member

Why is it creating a new Address when the map types are AddressModel and AddressDto? Is there some kind of Include call?

@cryo75
Copy link
Author

cryo75 commented Jul 1, 2016

@jbogard The reverse map looks correct as well.

@TylerCarlson1 There are no include calls. I'm actually creating a new AddressModel instance manually to skip EF:

                var model = new AddressModel()
                {
                    Id = 100,
                    Street = "str",
                    HouseNumber = "hn" //etc...
                };
                var dto= Mapper.Map<AddressDto>(model);

The actual mapping occurs in this method:

        TDestination IMapper.Map<TDestination>(object source)
        {
            if (source == null)
                return default(TDestination);

            var types = new TypePair(source.GetType(), typeof(TDestination));

            var func = _configurationProvider.GetUntypedMapperFunc(new MapRequest(types, types));

            //Added to debug...
            var map = _configurationProvider.FindTypeMapFor(types);

            return (TDestination) func(source, null, _defaultContext);
        }

The Target property of the func variable always includes less members than those expected.

@lbargaoanu
Copy link
Member

What about a nice gist, and we'll do the debugging? :)

@cryo75
Copy link
Author

cryo75 commented Jul 1, 2016

@lbargaoanu Tried that but the mapping worked because I had only a couple of mappings. I will give it another try.

@qswinson
Copy link

qswinson commented Jul 1, 2016

I'm having a similar issue with 5.0. The mapping used to work with 4.2.1 but now doesn't. Here's the gist that reproduces the problem https://gist.github.com/qswinson/20262e54ff597b757d7500a82301f4a9

@lbargaoanu
Copy link
Member

Where is GeoCoordinate?

@qswinson
Copy link

qswinson commented Jul 1, 2016

System.Device assembly. The example project contains a reference to it.

@lbargaoanu
Copy link
Member

A quick workaround is to disable constructor mapping if you don't use it.

@TylerCarlson1
Copy link
Member

@cryo75 So what is Address object and does it have any CreateMap in your configuration. And if so can you tell us what they are.

@qswinson
Copy link

qswinson commented Jul 1, 2016

@lbargaoanu How do you disable constructor mapping? I'm not finding that in the documentation.

@lbargaoanu
Copy link
Member

Choose an example :)

@jbogard jbogard added the Bug label Jul 6, 2016
@jbogard jbogard added this to the 5.0.1 milestone Jul 6, 2016
@jbogard jbogard closed this as completed in f3df254 Jul 6, 2016
@lock
Copy link

lock bot commented May 7, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants