A source member can map to multiple destination members #4002
-
Source/destination typesclass Order{
public int ProductId {get; set;}
public Product? Product {get; set;}
}
class OrderDto{
public int ProductId {get; set;}
} Mapping configuration// ASP Net Core Dependency Injection - Global configuration
builder.Services.AddAutoMapper((serviceProvider, config) => {
var postfixes = new[] { "Id", "Code" };
config.RecognizePostfixes(postfixes);
config.RecognizeDestinationPostfixes(postfixes);
config.AllowNullCollections = true;
config.AddCollectionMappers();
}, Assembly.GetExecutingAssembly());
// Actual map inside a profile
CreateMap<OrderDto, Order>(); Version: 11.0.1Expected behaviorI'd expect Automapper to match members with the same un-altered name first before proceeding to exclude pre/postfixes, so that ProductId of OrderDto gets mapped correctly to ProductId of Order. Actual behaviorI get an error saying missing mapping for int -> Product, because automapper tries to map Product(Id) to Product instead by removing the postfix. |
Beta Was this translation helpful? Give feedback.
Answered by
lbargaoanu
Jun 15, 2022
Replies: 1 comment 1 reply
-
Check the execution plan. You configured AM to map |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Xriuk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check the execution plan. You configured AM to map
ProductId
to bothProductId
andProduct
and, as the validation says, it cannot do that. It seems that kind of config is not exactly a good idea :)