Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework Include command - Left or inner join?

As I was investigating the difference between Include and Join I found that :

If the DB does not include a Foreign Keys -it has no navigation props so it's better to use Join

If It does have a navigation props - then use Include. ( it also save a db hit.)

But one answer here caught my attention:

Include is implemented as a join. Depending on the nullability of the included link it is an inner or left join.

Question :

How does the nullity affects the left / inner join ?

In Sql server I can have a Cities table and Persons table and a person can have a NULL CityID.

Why does entity Framework decides for me what kind of join it is ?

edit : visualization :

enter image description here

enter image description here

Now lets change CityId to not null :

enter image description here

And here is the change :

enter image description here

like image 833
Royi Namir Avatar asked Sep 15 '13 07:09

Royi Namir


People also ask

Can we use left outer join and inner join together?

The LEFT OUTER join will retrieve all rows from the table on the left of the join whether they match or not. In the subsequent INNER JOIN, obviously if something doesn't match up, you will lose some rows.

Should I use inner join or left join?

You'll use INNER JOIN when you want to return only records having pair on both sides, and you'll use LEFT JOIN when you need all records from the “left” table, no matter if they have pair in the “right” table or not.

Can inner join and left join giving same results?

The reason why LEFT JOIN and INNER JOIN results are the same is because all the records of table branch has at least one match on table user_mast . The main difference between INNER JOIN and LEFT JOIN is that LEFT JOIN still displays the records on the the LEFT side even if they have no match on the RIGHT side table.

Is Left join or inner join more efficient?

If the tables involved in the join operation are too small, say they have less than 10 records and the tables do not possess sufficient indexes to cover the query, in that case, the Left Join is generally faster than Inner Join. As you can see above, both the queries have returned the same result set.


2 Answers

Suppose that in your class there is a [Required] constraint on City or CityID. And suppose there are Person records without a (valid) City. The only way to satisfy the [Required] is to perform an inner join.

But as long as the constraints in your Db and model match (ie CityID INT NOT NULL) it wouldn't really matter what kind of Join is used. That should be the normal case.

And without the constraint you would of course expect a Left Join.

like image 190
Henk Holterman Avatar answered Oct 02 '22 14:10

Henk Holterman


I know this is an old question, but if anyone else lands here using EF Code First as I am, my issue was in the fluent mappings:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)     {         base.OnModelCreating(modelBuilder);          modelBuilder.Entity<Parent>()             .HasOptional(a => a.Child) /* LEFT OUTER JOIN */             .WithMany()             .HasForeignKey(a => a.ChildId);     } 

is translated as a LEFT OUTER JOIN whereas

    modelBuilder.Entity<Parent>()         .HasRequired(a => a.Child) /* INNER JOIN */         .WithMany()         .HasForeignKey(a => a.ChildId); 

is translated as an INNER JOIN.

like image 41
madannes Avatar answered Oct 02 '22 13:10

madannes