Using EF Core I'd like to fetch the foreign keys without a join with those tables.
I have two tables:
- Users [id, name, system(FK)]
- System [id, name]
I'd like to be able to fetch the foreign key for System from the Users table. I would have thought EF would be clever enough to remove the join if no other properties than the FK is selected but that's not what I'm seeing when I'm testing.
I've tried with:
var userSystem = DBContext.Users
.Where(a => a.Id == 1)
.Select(a => new { UserId = a.id, SystemId = a.system.Id })
.FirstOrDefault();
but looking at the generated SQL query it contains an unnecessary LEFT JOIN on the system table. Any ideas as to how I can make EF generate code without the join?
Note: The join is obviously not a problem in this example but I have other queries with lots of FK being fetched with strict performance requirements.