❔ one to zero or one relationship EFC
How can I model a 1:cn relationship in Entity Framework Core?
E.g. An employee either has a supervisor or not
12 Replies
https://learn.microsoft.com/en-us/ef/core/modeling/relationships explains a lot!
Relationships - EF Core
How to configure relationships between entity types when using Entity Framework Core
you might just need
And it will create a FK for you
if i do
_db.Employees.Include(e => e.Manager)
it filters out employees without managers. how can you avoid that?it doesn't filter out anything
it makes is to when the query runs, it will also fetch the person's managers
if you omit the
Include
, the .Manager
will be null for everybody, because EF by default does not load all references
if it would always do that, it would make a lot of queries slow when the properties are not usedit uses an inner join in the compiled sql query apparently, that would explain why its filtered for me
that is very odd
only difference is that i have the new-ish #nullable disabled so i dont have the question mark after the type name
I'm pretty sure it does not inner join
linqpad shows a left join
you can output the generated SQL on an IQueryable<T> with
.ToQueryString()
now it works correctly, weird.
i swear yesterday it didnt
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.