I am using Entity Framework Core 2.0 and have this 2 classes:
news
public class News
{
public int Id { get; set; }
public string Title{ get; set; }
public string Text { get; set; }
public DateTime ReleaseDate{ get; set; }
public int AuthorID{ get; set; }
public Author Author{ get; set; }
}
Author
public class Author
{
public Author ()
{
News = new List<News>();
}
public int Id { get; set; }
public string Username{ get; set; }
public string Firstname{ get; set; }
public string Lastname{ get; set; }
public ICollection<News> News {get;set;}
}
I want to download the news with the author’s username only because I do not need other information, such as the author’s name and name.
If I do this:
static void Main(string[] args)
{
using (var ctx = new DbContext())
{
var result= ctx.News.Include(x => x.Author).Select(news => new
{
news= news ,
Username= pr.Author.Username
}).ToList();
}
}
Entity Framework Core 2.0 generates a database query, for example:
SELECT [x].[ID], [x].[Text], [x].[Title], [x].[AuthorId], [x].[ReleaseDate], [x.Author].[ID], [x.Verfasser].[Username] AS [Username], [x.Verfasser].[Firstname], [x.Verfasser].[Lastname]
FROM [News] AS [x]
INNER JOIN [Author] AS [x.Author] ON [x].[AuthorId] = [x.Author].[ID]
Is there a way to achieve the expected behavior?
source
share