EF DBContext - 查询加载超出我想要的数量

时间:2014-03-28 17:44:57

标签: c# sql entity-framework linq-to-sql dbcontext

我在我的存储库中有这个代码。

我想加载Place对象及其依赖项(它在 PlaceCompleteQuery 中定义)。 Type有属性TypeTranslations。

方法 GetPlaceAsync 应按Id和Source加载 Place ,并且只应加载 TypeTranslation 的一个实例以加载类型(调用方法 LoadTranslationsAsync (...) - 这个方法实际上只加载了一个 TypeTranslation 的实例。

    private IQueryable<Place> placeCompleteQuery;
    protected IQueryable<Place> PlaceCompleteQuery
    {
        get
        {
            return this.placeCompleteQuery ?? (this.placeCompleteQuery = this.DbContext.Places.Include(p => p.Address)
                .Include(p => p.CreatedBy)
                .Include(p => p.Source)
                .Include(p => p.Type)
                .Include(p => p.ExternalMappings));
        }
    }

    public async Task<Place> GetPlaceAsync(string id, string source, int languageId)
    {
        IQueryable<Place> query =
            PlaceCompleteQuery.Where(p => p.ExternalMappings.Any(m => m.Source.Name.ToLower() == source.ToLower() && m.ExternalId == id));

        Place result = await query.SingleOrDefaultAsync();

        await this.LoadTranslationsAsync(languageId, result);

        return result;
    }

    private async Task LoadTranslationsAsync(int languageId, Place result)
    {
        await this.DbContext.PlaceTypeTranslations.Where(t => t.LanguageId == languageId && t.PlaceTypeId == result.Type.Id)
            .LoadAsync();
    }

我的问题是,在我尝试调用方法 LoadTranslationsAsync (...)之前,已经为所有可用语言加载了Type的 TypesTranslation

这是我用SqlProfiler捕获的SQL命令:

exec sp_executesql N'SELECT 
  [Project3].[Id] AS [Id], 
  [Project3].[Discriminator] AS [Discriminator], 
  [Project3].[Name] AS [Name], 
  [Project3].[PlaceTypeId] AS [PlaceTypeId], 
  [Project3].[SourceId] AS [SourceId], 
  [Project3].[AddressId] AS [AddressId], 
  [Project3].[CreateByUserId] AS [CreateByUserId], 
  [Project3].[Location] AS [Location], 
  [Project3].[AvgRate] AS [AvgRate], 
  [Project3].[Url] AS [Url], 
  [Project3].[Mail] AS [Mail], 
  [Project3].[Phone] AS [Phone], 
  [Project3].[CreateDate] AS [CreateDate], 
  [Project3].[Id1] AS [Id1], 
  [Project3].[Street] AS [Street], 
  [Project3].[StreetNo] AS [StreetNo], 
  [Project3].[City] AS [City], 
  [Project3].[PostalCode] AS [PostalCode], 
  [Project3].[Country] AS [Country], 
  [Project3].[Id2] AS [Id2], 
  [Project3].[Name1] AS [Name1], 
  [Project3].[NickName] AS [NickName], 
  [Project3].[CreateDate1] AS [CreateDate1], 
  [Project3].[Id3] AS [Id3], 
  [Project3].[Name2] AS [Name2], 
  [Project3].[Description] AS [Description], 
  [Project3].[C1] AS [C1], 
  [Project3].[Id4] AS [Id4], 
  [Project3].[PlaceId] AS [PlaceId], 
  [Project3].[ExternalId] AS [ExternalId], 
  [Project3].[SourceId1] AS [SourceId1]
  FROM ( SELECT 
      [Limit1].[Id] AS [Id], 
      [Limit1].[Name] AS [Name], 
      [Limit1].[PlaceTypeId] AS [PlaceTypeId], 
      [Limit1].[SourceId] AS [SourceId], 
      [Limit1].[AddressId] AS [AddressId], 
      [Limit1].[CreateByUserId] AS [CreateByUserId], 
      [Limit1].[Location] AS [Location], 
      [Limit1].[AvgRate] AS [AvgRate], 
      [Limit1].[Url] AS [Url], 
      [Limit1].[Mail] AS [Mail], 
      [Limit1].[Phone] AS [Phone], 
      [Limit1].[CreateDate] AS [CreateDate], 
      [Limit1].[Discriminator] AS [Discriminator], 
      [Limit1].[Id1] AS [Id1], 
      [Limit1].[Street] AS [Street], 
      [Limit1].[StreetNo] AS [StreetNo], 
      [Limit1].[City] AS [City], 
      [Limit1].[PostalCode] AS [PostalCode], 
      [Limit1].[Country] AS [Country], 
      [Limit1].[Id2] AS [Id2], 
      [Limit1].[Name1] AS [Name1], 
      [Limit1].[NickName] AS [NickName], 
      [Limit1].[CreateDate1] AS [CreateDate1], 
      [Limit1].[Id3] AS [Id3], 
      [Limit1].[Name2] AS [Name2], 
      [Limit1].[Description] AS [Description], 
      [Extent7].[Id] AS [Id4], 
      [Extent7].[PlaceId] AS [PlaceId], 
      [Extent7].[ExternalId] AS [ExternalId], 
      [Extent7].[SourceId] AS [SourceId1], 
      CASE WHEN ([Extent7].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
      FROM   (SELECT TOP (2) 
          [Extent1].[Id] AS [Id], 
          [Extent1].[Name] AS [Name], 
          [Extent1].[PlaceTypeId] AS [PlaceTypeId], 
          [Extent1].[SourceId] AS [SourceId], 
          [Extent1].[AddressId] AS [AddressId], 
          [Extent1].[CreateByUserId] AS [CreateByUserId], 
          [Extent1].[Location] AS [Location], 
          [Extent1].[AvgRate] AS [AvgRate], 
          [Extent1].[Url] AS [Url], 
          [Extent1].[Mail] AS [Mail], 
          [Extent1].[Phone] AS [Phone], 
          [Extent1].[CreateDate] AS [CreateDate], 
          [Extent1].[Discriminator] AS [Discriminator], 
          [Extent2].[Id] AS [Id1], 
          [Extent2].[Street] AS [Street], 
          [Extent2].[StreetNo] AS [StreetNo], 
          [Extent2].[City] AS [City], 
          [Extent2].[PostalCode] AS [PostalCode], 
          [Extent2].[Country] AS [Country],   
          [Extent3].[Id] AS [Id2], 
          [Extent3].[Name] AS [Name1], 
          [Extent3].[NickName] AS [NickName], 
          [Extent3].[CreateDate] AS [CreateDate1], 
          [Extent4].[Id] AS [Id3], 
          [Extent4].[Name] AS [Name2], 
          [Extent4].[Description] AS [Description]
          FROM    [dbo].[Places] AS [Extent1]
          INNER JOIN [dbo].[Addresses] AS [Extent2] ON [Extent1].[AddressId] = [Extent2].[Id]
          INNER JOIN [dbo].[Users] AS [Extent3] ON [Extent1].[CreateByUserId] = [Extent3].[Id]
          INNER JOIN [dbo].[Sources] AS [Extent4] ON [Extent1].[SourceId] = [Extent4].[Id]
          WHERE ([Extent1].[Discriminator] IN (N''PlaceDto'',N''Place'')) AND ( EXISTS (SELECT 
              1 AS [C1]
              FROM  [dbo].[ExternalMappings] AS [Extent5]
              INNER JOIN [dbo].[Sources] AS [Extent6] ON [Extent5].[SourceId] = [Extent6].[Id]
              WHERE ([Extent1].[Id] = [Extent5].[PlaceId]) AND ((((LOWER([Extent6].[Name])) = (LOWER(@p__linq__0))) AND ( NOT ((LOWER([Extent6].[Name]) IS NULL) OR (LOWER(@p__linq__0) IS NULL)))) OR ((LOWER([Extent6].[Name]) IS NULL) AND (LOWER(@p__linq__0) IS NULL))) AND ([Extent5].[ExternalId] = @p__linq__1) AND (@p__linq__1 IS NOT NULL)
        )) ) AS [Limit1]
   LEFT OUTER JOIN [dbo].[ExternalMappings] AS [Extent7] ON [Limit1].[Id] = [Extent7].[PlaceId]
)  AS [Project3]
  ORDER BY [Project3].[Id] ASC, [Project3].[Id1] ASC, [Project3].[Id2] ASC, [Project3].[Id3] ASC, [Project3].[PlaceTypeId] ASC, [Project3].[C1] ASC',N'@p__linq__0 nvarchar(4000),@p__linq__1 nvarchar(4000)',@p__linq__0=N'FourSquare',@p__linq__1=N'1'

如果我在Managament Studio中对数据库执行SQL命令 - 结果只有一行。 SQL命令不包含任何TypeTranslation。

执行查询之前的我的上下文: enter image description here

我执行查询后的结果变量: enter image description here

我的问题是导致不良行为的原因是什么?

0 个答案:

没有答案