question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapping to existing object of same type returns the source instead of modifying the destination

See original GitHub issue

EDIT: actually, the problem is not related to CreateMissingTypeMaps; see comments below

Just noticed a bug where CreateMissingTypeMaps = true has no effect when you pass an existing destination object to Mapper.Map. Here’s a quick repro using LinqPad:

void Main()
{
    var foo1 = new Foo { X = 42, Y = 123, Bar = new Bar { Z = 999 } };
    var foo2 = new Foo();
    var foo3 = Mapper.Map<Foo, Foo>(foo1, foo2, o => o.CreateMissingTypeMaps = true);
    Console.WriteLine ("foo3 == foo1: {0}", foo3 == foo1); // true; expected false
    Console.WriteLine ("foo3 == foo2: {0}", foo3 == foo2); // false; expected true
    Console.WriteLine (foo2.ToString()); // Foo(X = 0, Y = 0, Bar = ); expected Foo(X = 42, Y = 123, Bar = Bar(Z = 999))
}

class Foo
{
    public int X { get; set; }
    public int Y { get; set; }
    public Bar Bar { get; set; }

    public override string ToString() { return string.Format("Foo(X = {0}, Y = {1}, Bar = {2})", X, Y, Bar); }
}

class Bar
{
    public int Z { get; set; }
    public override string ToString() { return string.Format("Bar(Z = {0})", Z); }
}

Mapping to a new instance with Mapper.Map<Foo>(foo1) works as expected.

Also, if I explicitly create the map for Foo -> Foo, then the map for Bar -> Bar is correctly created, so it seems the problem only affects the root of the mapping.

Another issue (perhaps unrelated) that bothers me is that the mapping fails silently… the destination object is left untouched, and Mapper.Map just returns the source object (it returns the destination object when the mapping succeeds). Shouldn’t it throw an exception?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
thomaslevesquecommented, Dec 30, 2014

OK, I think I know what’s going on; if there is no map defined for Foo -> Foo, AutoMapper uses AssignableMapper, which just returns the source value. But in the case of a mapping with destination, it doesn’t make sense: I explicitly want to copy the source object’s properties to the destination object…

1reaction
lioobayoyocommented, Oct 16, 2018

Ok, so to use map to auto-assign same properties on destination object of type A from source type A, I can create a profile :

CreateMap<A,A>();

But imagine that I want to be able to use this on my 100 classes likewise. I’ll get something like :

CreateMap<A,A>();
CreateMap<B,B>();
CreateMap<C,C>();
// ...

Is there another “state of the Automapper art” way of doing it ? For instance in the .Map(source, destination, ????) call directly ? (my goal is to prevent silent logical errors if I forget to add the dummy <X, X> profile for my 99th class X)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automapper: Update property values without creating a ...
Use the overload that takes the existing destination: Mapper.Map<Source, Destination>(source, destination);. Yes, it returns the destination ...
Read more >
Mapping same type results in reference to source object ...
Basically, if the source and destination type are the same, AutoMapper does not create a new object, nor does it populate an existing...
Read more >
AutoMapper Madness - Nuances in Self-Mapping - Blog
There's no trickery involved here: Just call CreateMap() referencing the same type for both the source and destination: CreateMap<ExampleClass, ...
Read more >
Getting Started with AutoMapper in ASP.NET Core
The object-to-object mapping works by transforming an input object of one type into an output object of a different type.
Read more >
Best Practices for Updating Existing Collections?
implement Equals in my DTO as the source of the mapping so that AutoMapper will see that the objects are the same and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found