One-to-many relationships - EF
Hi guys might be a silly question but im confused, if i had a relationship like this
when i query posts i still get the data blog without using include. should i confiure the default behavior to no tracking?
8 Replies
normally in order to fetch the navigation property as well you would need to use the include :
so if i do something like this
var postsIncludingBlog = _context.Posts.ToList();
blog will be null?
yes
when im testing it in my code im getting the values
without using include
strange..are you passing the blog explicitly while creating the
Post
?no i was just querying the table directly
I overlooked this part and i think this is whats happening correct me if im wrong.
in the same code block i just noticed it was first querying blogs table
then later it queries posts
so it wasn't null because blogs data is available in the context?
If the data is being tracked by the change tracker of your current context instance it will be filled from there.
So if you test data fetching or even any kind of manipulation with an DBContext, you should either call context.ChangeTracker.Clear() or create a new context between operations.
understood thank you guys!
:meowheart: