I have this structure in SQL Server:
Look at the expanded tables.
CampoAdicionalPorPersona
table is a navigation table between Persona
and CampoAdicional
(many to many relationship).
When I do this query in EF Core Linq:
await db.Persona
.Include(p => p.CentroCosto)
.Include(p => p.Departamento)
.Include(p => p.Cargo)
.Include(p => p.CampoAdicionalPorPersona)
.FirstOrDefaultAsync(p => p.PersonaEliminadoEn == null && p.PersonaId == id);
Then query is translated by Entity Framework Core to this SQL:
SELECT [c1].[CampoId], [c1].[PersonaId], [c1].[CampoValorFecha], [c1].[CampoValorNumero], [c1].[CampoValorTexto], [c1].[ExtCiudadId], [c1].[OpcionId], [t].[PersonaId], [t].[CentroCostoId], [t].[DepartamentoId], [t].[CargoId]
FROM (
SELECT TOP(1) [p].[PersonaId], [c].[CentroCostoId], [d].[DepartamentoId], [c0].[CargoId]
FROM [Persona] AS [p]
LEFT JOIN [CentroCosto] AS [c] ON [p].[CentroCostoId] = [c].[CentroCostoId]
LEFT JOIN [Departamento] AS [d] ON [p].[DepartamentoId] = [d].[DepartamentoId]
LEFT JOIN [Cargo] AS [c0] ON [p].[CargoId] = [c0].[CargoId]
WHERE [p].[ExtEmpresaId] IN (
SELECT [a].[value]
FROM OPENJSON(@__allowed_0) WITH ([value] int '$') AS [a]
) AND [p].[PersonaEliminadoEn] IS NULL AND [p].[PersonaId] = @__id_1
) AS [t]
INNER JOIN [CampoAdicionalPorPersona] AS [c1] ON [t].[PersonaId] = [c1].[PersonaId]
ORDER BY [t].[PersonaId], [t].[CentroCostoId], [t].[DepartamentoId], [t].[CargoId]
Since CampoAdicionalPorPersona
is a navigation table (for a many to many relationship), why is it translated as INNER JOIN
instead of LEFT JOIN
as the other relationships in the query?
Personal
entity can have 0 to many CampoAdicionalPorPersona
. If there are no elements, query result is empty.
How can I solve this?
5