What I wrote about in the article, I caught about 10 hours, it was 10 hours of continuous debugging, which resulted in a step-by-step comparison of the working and non-working versions of the code, even not so, compared to each line from the debugging window of the working and non-working versions of the code
For a programmer who is not familiar with expression trees, a good introductory article on msdn , it details in detail some steps in building a tree, which I omitted in the article.
In which case this test will be green (the EF add-on is enabled, at which it crashes, if it cannot fully translate the query to SQL ).
Secondly, the provider must parse this mapping, so, for example, it will not work, EF will start swearing (by default it does not swear, it puts the entities into memory - LINQ to Object , but this can be enabled ):
And the thing is that Query Provider does not understand by what property (properties) of the entity we want to sort. But if the mapping is written correctly, the provider will cope, send a SQL request, get an answer and deserialize the results.
Sometimes there is a need to write Expression'ov itself, that is, something similar:
I'll show you how to write this Expression , it's not that difficult, the link above gives some similar examples:
We step by step collect a lambda, first a parameter, a property, and then we glue everything together.
In which case it will not work and the test will be green (the provider will not be able to parse the expression)?
Do not know? Do not worry, I did not know either, and in the end I spent about 10 hours to understand.
Here's a hint, so it will work:
The thing is, PropertyInfo needs to be taken from the base class if the PupilName property itself belongs to the base class, so C # builds a lambda expression , and EF parses them, relying on the language standards
What debugger shows if you write a regular lambda in OrderBy :
And in our case this way:
If you try not to duplicate the code, you are surely faced with the inheritance of DTO and IncludeBase :
It happens even more sophisticated situation:
And this test will be green:
And the point is the same, again PropertyInfo is pulled from the interface:
Age's ReflectedType property is an interface, IPupilDto , C # builds a lambda, in which the Age property is a property of the PupilDto class, not the interface, but how the auto-lambda builds a lambda:
How to solve this problem? If the IncludeBase Automapper with the interface does not suit us (if you use mapping in memory - this will not affect you), then you will have to abandon this API, I solved this problem by highlighting the mapping in the extension method , like this:
Then the authamper will find the appropriate type property by name, build the "correct" lambda.
Thanks for attention!
Source: https://habr.com/ru/post/453720/
All Articles