Closed davidsavagejr closed 1 year ago
A PR is welcome.
This appears to be related to: https://github.com/AutoMapper/AutoMapper/issues/3526 except its popping up in the Collections init.
Honestly not sure where to slap the validation in for this but what it turned out to be was this:
public interface ISomeInterface {}
public class SomeBaseClass : ISomeInterface {}
public class ProblemClass : SomeBaseClass {}
When the mapper initializes it tries to construct SomeBaseClass
for the expression to add items to the collection; which obviously blows up because you can't add items of that type to a collection of ProblemClass
In previous versions of Automapper/Automapper Collection it appeared to allow you to do this and would pick up on your inheritance...
CreateMap<..., ProblemClass>()
.IncludeBase<.., ISomeInterface>()
Solution There's actually a missing mapping:
CreateMap<SomeBaseClass, ProblemClass>();
Once this mapping is in place, I had no issues and my configuration would construct and assert as valid.
Good to know :)
Upgrading From Automapper: 10.1.1 Automapper.Collection: 7.0.1 net 6.0
Upgrading To Automapper: 12.0.1 Automapper.Collection: 9.0.0 net 7.0
I have a working project right now that I'm upgrading so, all I've done so far is simply upgrade the packages (see above). I'm running into an issue when the Automapper configuration tries to get constructed:
OneTimeSetUp: System.ArgumentException : Expression of type 'Type1' cannot be used for parameter of type 'Type2' of method 'Void Add(Type2)' (Parameter 'arg0')
Stack Trace
When I remove the following line:
cfg.AddCollectionMappers();
The above exception goes away however, I obviously run into mapping issues in my application where collection mappings happen (incorrectly); My configuration is built by simply scanning all my assemblies for
Profile
and adding them all to the configuration.I am using Lamar IoC:
The only other information I can think to offer are my class structures and mapping configurations
Based on the stack trace, I'm unsure if there is a mapping issue I'm missing or not