IFrizy
IFrizy

Reputation: 1745

Difference between Include in EF and Join in SQL

I have some distraction when i speak with my collegue and he said that Include in EF and Join in SQL is different things. Can you explain is it true? If yes, explaint in what difference is?

Thanks.

Upvotes: 2

Views: 5476

Answers (1)

Shekhar
Shekhar

Reputation: 832

When querying EF through linq or lambda expressions, you only need join statements if the underlying schema doesn't provide FKs, and thus you don't have navigation properties on the objects.

On the other side, include (eager loading) and lazy loading can only work if there are FKs, because it uses the navigation properties.

The underlying sql in both cases will use joins (as sql has no "navigation property" concept).

As for performance, it depends on situations. Lazy loading vs Eager loading (so in FK scenario) can be a difficult choice.

I usually go with lazy loading, useful when you have a large main result, but you need "join" data only of a few items of the whole resultset.

If you know ahead that you'll need the join data of the whole resultset, eager loading could be better for performance. I'd suggest to experiment and see for yourself.

Taken from Understanding EF. Include vs Joins

Upvotes: 4

Related Questions