Hy everybody, i'm new here, despite i'm reading stackoverflow since a couple of years.
I'm currently working on a .Net 5 web application that reads data from a sql server database and serves them through webApi.
Today, during some tests, i encountered a strange (for me) behaviour.
When i tried to read (just read! no editing or adding) data from two tables, that are related each other by a many-to-many relationship, the query give me only the top-table data, and zero entries of the related table.
At first look i thought was a question related to async/await pattern, but same behaviour was found using syncronous methods.
After i found that if i remove the AsNoTracking()
method from the query, everything works as expected.
I can't undestand why.
Asked
Active
Viewed 29 times
0
-
Read this: https://stackoverflow.com/questions/12211680/what-difference-does-asnotracking-make – D A Nov 17 '21 at 13:22
-
1Hi, providing small reproducible example (model classes/configuration and the reading code) would help identifying the problem. Because the behavior you are describing is definitely not "by design", so it's either a EFC bug or incorrect usage or side effect. Also specify the EF Core version. – Ivan Stoev Nov 17 '21 at 13:35
-
Hi, after further investigation and tests i think i figured out what's the problem. Basically it's related to the different way EF core tracks the entities, despite EF 5 i previously used. I didn't know that related entities, by default, wasn't tracked. Adding the dbset<>.Include([related entity]) to my query solved the problem, also if i don't use the change tracker. Thank you! – Marco Macchione Nov 18 '21 at 08:12