I wrote some code for one to many relationships getting Invoice
instances from Defect
instances.
mydbEntities ef = new mydbEntities (); //mydbEntities is derived from DbContext
ef.Database.Log = s => System.Diagnostics.Debug.WriteLine (s);
Invoice inv = ef.Defects.Where (i => i.Id == 5).SingleOrDefault ().Invoice;
That is part of EDM diagram:
I'm curious why it did not invoke INNER JOIN, instead of it 2 SQL queries were executed. In the book I'm now reading ("Mastering Entity Framework"), in the same situation an INNER JOIN was invoked.
Output from Debug:
SELECT TOP (2)
[Extent1].[Id] AS [Id],
[Extent1].[PositionId] AS [PositionId],
[Extent1].[InvoiceId] AS [InvoiceId],
[Extent1].[Count] AS [Count],
[Extent1].[Reason] AS [Reason]
FROM [dbo].[Defect] AS [Extent1]
WHERE 5 = [Extent1].[Id]
-- Executing at 6/21/2015 11:21:02 AM +05:00
-- Completed in 1 ms with result: SqlDataReader
Closed connection at 6/21/2015 11:21:02 AM +05:00
Opened connection at 6/21/2015 11:21:02 AM +05:00
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Number] AS [Number],
[Extent1].[InvoiceDate] AS [InvoiceDate]
FROM [dbo].[Invoice] AS [Extent1]
WHERE [Extent1].[Id] = @EntityKeyValue1
-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)
-- Executing at 6/21/2015 11:21:02 AM +05:00
-- Completed in 0 ms with result: SqlDataReader
Closed connection at 6/21/2015 11:21:02 AM +05:00
And one more question: How do 2 queries against 1 with INNER JOIN impact on performance in high-loaded application with large database.