实体框架嵌套包含

时间:2013-05-02 08:55:39

标签: c# entity-framework entity-framework-6

假设我有三个实体:

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的{​​{1}}条记录为空,我会像这样查询:

Kecamatan

Ef将生成如下查询:

_context
    .EkstrakurikulerPembimbing
    .Include(x => x.Kecamatan)
    .Include(x => x.Kecamatan.Kota)
    .Where(x => x.Ekstrakurikuler.Id == someInt)
    .ToList();

由于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(与实体mst_kota相关),因此根本不会返回任何数据。

即使Kota中的Kecamatan为空,如果查询它们以返回数据,如果EkstrakurikulerPembimbing不为空,它还是会急切加载?

0 个答案:

没有答案