Should I use navigation properties in EF Core?
When using EF Core should I always use navigation properties or never?
for example I have:
I use .NET 9 in my smaller project and I wonder if it's better to use it or not in terms of performance?
14 Replies
navigation properties are a major part of EF, if your entities have a relationship you conventionally model it by defining navigation properties
but as i've seen there's an option to omit them
yes, you don't have to use them
and I just wondered if it's good idea, better or what
You can have both, which is what I do.
it's much more convenient to use navigation properties in a query than writing explicit joins
and should i use
virtual
aswell?no performance differences to mention that i'm aware of
it uses lazy loading then, right?
no, that's related to lazy loading which you should never use
oh, why not? 🤔
because it causes surprise synchronous IO when you're accessing properties of what looks like a normal C# object
ideally you use a .Select in your query to project out just the data you actually want, all in one go
alright, thanks i guess 😅 that explains a bit
^ this is also valid in EF, gives you the ability to reference nav properties in your queries, but also explicitly use the relational columns if you want to