Reputation: 9888
I've found two solution to ignore non mapped properties but one of them also ignore the conventional mappings, and the second doesn't work with QueryableExtensions to return an IQueryable (don't know why but I get the error Argument types do not match).
Does anyone has a solution to ignore non mapped properties that covers both aspects above?
Upvotes: 0
Views: 1666
Reputation: 49
Chaining at the end of the mapping with the .ForAllOtherMembers(opt => opt.Ignore());
worked for me. This should be the last method in the method chain.
Upvotes: 2
Reputation: 6026
When using QueryableExtensions you have to be explicit with some type conversions, such as int?
to int
. This is probably the source of the "Argument types do not match" exception.
If you have many properties that need a type conversion -- like if you had many other properties where you find you are doing c.MyVariable ?? 0
-- you can instead define a conversion rule and not have to be explicit about every property.
Normally, to do type conversions in Automapper, you would use ConvertUsing
but when using QueryableExtensions, you need to instead use ProjectUsing
.
You could use the following line and it will take care of all mappings from int?
to int
without the need to explicitly specify the mappings for each property:
cfg.CreateMap<int?, int>().ProjectUsing(src => src.HasValue ? src.Value : 0);
Upvotes: 3
Reputation: 9888
Problem solved. It was in this line of code
.ForMember(p => p.Gender, opt => opt.MapFrom(c => c.GenderCode))
where p.Gender was of type int and GenderCode of type int?. so changing to
.ForMember(p => p.Gender, opt => opt.MapFrom(c => c.GenderCode ?? 0))
fixed the problem. What made it hard to troubleshoot is that the mapping code above was working until I try to return IQueryable.
Upvotes: 1