如何在执行JOIN时使Entity Framework Core查询某些字段

时间:2017-01-16 13:22:54

标签: c# entity-framework linq entity-framework-core

我正在使用LINQ来查询来自多个表的数据。

var test= await _context.Article
.Select(a => new
{
    a.ID,
    a.Title,
    a.CreatedTime,
    a.EditedTime,
    a.Author.UserName,
    a.AuthorID
}).ToListAsync();

AuthorArticle类中的导航属性。

但我从日志中得到了这个:

Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [a].[ID], [a].[AuthorID], [a].[Content], [a].[CreatedTime], [a].[EditedTime], [a].[Title], [a.Author].[Id], [a.Author].[AccessFailedCount], [a.Author].[ConcurrencyStamp], [a.Author].[Email], [a.Author].[EmailConfirmed], [a.Author].[LockoutEnabled], [a.Author].[LockoutEnd], [a.Author].[NormalizedEmail], [a.Author].[NormalizedUserName], [a.Author].[PasswordHash], [a.Author].[PhoneNumber], [a.Author].[PhoneNumberConfirmed], [a.Author].[SecurityStamp], [a.Author].[TwoFactorEnabled], [a.Author].[UserName]
      FROM [Article] AS [a]
      LEFT JOIN [AspNetUsers] AS [a.Author] ON [a].[AuthorID] = [a.Author].[Id]
      ORDER BY [a].[AuthorID]

我只想要一些字段,但它会查询所有字段。如果我从代码中删除a.Author.UserName行,它就会按预期工作。

我错过了什么吗?

我正在使用Entity Framework Core 1.1.0和Microsoft SQL Server。

1 个答案:

答案 0 :(得分:1)

此行为已经documented as bug,修复了里程碑2.0.0的计划