Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when mapping same value with AutoMapper

Tags:

c#

automapper

I've got this code which fails:

internal class Program
{
    private static void Main()
    {
        Mapper.CreateMap<SourceFoo, TargetFoo>();
        Mapper.CreateMap<string, Stuff>()
            .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src))
            .ForMember(dest => dest.IgnoreMe, opt => opt.Ignore());

        var source = new SourceFoo
            {
                Stuff = "a",
                Stuff2 = "a"
            };
        var target = new TargetFoo
            {
                Stuff = new Stuff(),
                Stuff2 = new Stuff()
            };
        Mapper.Map(source, target);

        Console.WriteLine(target.Stuff.Value);
        Console.WriteLine(target.Stuff2.Value);
        Console.ReadLine();
    }
}

public class SourceFoo
{
    public string Stuff { get; set; }

    public string Stuff2 { get; set; }
}

public class TargetFoo
{
    public Stuff Stuff { get; set; }

    public Stuff Stuff2 { get; set; }
}

public class Stuff
{
    public string Value { get; set; }
    public bool IgnoreMe { get; set; }
}

When Stuff/Stuff2 has the same value I get this exception:

An item with the same key has already been added.

If they have different values everything works. I use AutoMapper 2.2.0.

Have I done something wrong or is it a bug? How can I solve it?

like image 213
Allrameest Avatar asked Dec 07 '12 17:12

Allrameest


1 Answers

It's a bug.

Now you can install version 2.1.267.

NuGet Package Manager Console commands:

Uninstall-Package AutoMapper -Force

Install-Package AutoMapper -Version 2.1.267

Check this links:

https://github.com/AutoMapper/AutoMapper/issues/250

And in milestone: 2.2.1 issue #270

like image 176
kmatyaszek Avatar answered Oct 09 '22 14:10

kmatyaszek