假设我有三个实体:
public class Kecamatan
{
public int Id { get; set; }
[Required]
public string Nama { get; set; }
[Required]
public virtual Kota Kota { get; set; }
}
public class Kota
{
public int Id { get; set; }
[Required]
public string Nama { get; set; }
[Required]
public virtual Propinsi Provinsi { get; set; }
}
public class EkstrakurikulerPembimbing
{
public int Id { get; set; }
public string Nama { get; set; }
public string Alamat { get; set; }
public string NoHp { get; set; }
public virtual Kecamatan Kecamatan { get; set; }
[Required]
public virtual Ekstrakurikuler Ekstrakurikuler {get; set;}
}
当我有为空的EkstrakurikulerPembimbing
记录时Kecamatan
,我像这样查询它们:
_context
.EkstrakurikulerPembimbing
.Include(x => x.Kecamatan)
.Include(x => x.Kecamatan.Kota)
.Where(x => x.Ekstrakurikuler.Id == someInt)
.ToList();
Ef 将生成如下查询:
EXEC sp_executesql N'SELECT
[Extent1].[id] AS [id],
[Extent1].[nama] AS [nama],
[Extent1].[alamat] AS [alamat],
[Extent1].[no_hp] AS [no_hp],
[Extent2].[id] AS [id1],
[Extent2].[nama] AS [nama1],
[Extent2].[kota_id] AS [kota_id],
[Extent3].[id] AS [id2],
[Extent3].[nama] AS [nama2],
[Extent3].[provinsi_id] AS [provinsi_id],
[Extent1].[Ekstrakurikuler_Id] AS [Ekstrakurikuler_Id]
FROM [dbo].[ekstrakurikuler_pembimbing] AS [Extent1]
LEFT OUTER JOIN [dbo].[kecamatan] AS [Extent2] ON [Extent1].[Kecamatan_Id] = [Extent2].[id]
INNER JOIN [dbo].[mst_kota] AS [Extent3] ON [Extent2].[kota_id] = [Extent3].[id]
WHERE [Extent1].[Ekstrakurikuler_Id] = @p__linq__0', N'@p__linq__0 int', @p__linq__0 = 1
由于INNER JOIN
to table mst_kota
(与 entity 相关Kota
),根本没有返回数据。
即使Kecamatan
inEkstrakurikulerPembimbing
为null,如何查询它们以返回数据,如果Kecamatan
不为null,它会渴望加载?