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

Simple ProjectTo throws Exception, stating an incorrect Property type #1875

Closed
pothos-dev opened this issue Jan 1, 2017 · 2 comments
Closed
Milestone

Comments

@pothos-dev
Copy link

When I use this simple test program:

using System;
using System.Linq;
using AutoMapper;
using AutoMapper.QueryableExtensions;

public class Foo
{
    public int A;
}

public class FooDto
{
    public int A;
}

public class Program
{
    public static void Main(string[] args)
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<Foo, FooDto>();
        });

        var arr = Enumerable
            .Range(0, 10)
            .Select(i => new Foo {A = i})
            .AsQueryable()
            .ProjectTo<FooDto>()
            .ToArray();

        Console.WriteLine(arr[7].A);
    }
}

The query fails with an AutoMapperMappingException.

Unable to create a map expression from Foo.A (Foo) to FooDto.A (System.Int32)

Mapping types:
Foo -> FooDto
Foo -> FooDto

Type Map configuration:
Foo -> FooDto
Foo -> FooDto

Property:
A

Foo.A (an int) is called a Foo by the exception.
The PropertyMap correctly identifies the SourceType as System.Int32.
Mapper.Map<FooDto>(new Foo()) also works as expected.
It does not matter if I use the static configuration method or create an instance of mapper and pass it to ProjectTo().

If I adjust the configuration like this:

        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<Foo, FooDto>().ForMember(
                dto => dto.A,
                conf => conf.MapFrom(foo => foo.A));
        });

The error goes away.

@lbargaoanu
Copy link
Member

It's a bug, but properties work.

@jbogard jbogard added this to the vNext milestone Jan 3, 2017
@jbogard jbogard closed this as completed in 393bd4f Jan 3, 2017
jbogard added a commit that referenced this issue Jan 3, 2017
@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.
Projects
None yet
Development

No branches or pull requests

3 participants