admin管理员组文章数量:1400982
I created DTOs with GraphQL to avoid exposing entities outside of my project and also we have REST APIs and entity AppDbContext in our existing project . While the DTOs return the requested data
{
"data": {
"stocks": [
{
"symbol": "hij"
},
{
"symbol": "abc"
},
but it selects all the properties in the select list and filters out in memory, resulting in bad SQL queries that select all columns.
I request just symbols:
{
stocks {
symbol
}
}
The query generated is as shown below in efficiently selects 'created'
SELECT [c].[symbol] AS [Symbol], [c].[created] AS [Created]
FROM [company] AS [c]
C# .NET and Entity Framework Core code
[UseFiltering]
[UseSorting]
public IQueryable<StockResponseDTO> GetStocks([Service] ICompanyRepository stockRepository)
{
return stockRepository.ReadAll().Select(stock => new StockResponseDTO
{
Symbol = stock.Symbol,
Created = stock.Created,
});
}
public class StockResponseDTO
{
public string Symbol { get; set; }
public DateTime? Created { get; set; } // Nullable to handle missing values
}
And also if I request like below, it selects all columns in efficient query
{
stocks {
symbol,
isin
}
}
Repo
public IQueryable<EntityFramework.App.Company.Company> ReadAll()
{
return _dbContext.Company;
}
[UseFiltering]
[UseSorting]
public IQueryable<App.EntityFramework.App.Company.Company> GetStocks([Service] ICompanyRepository stockRepository)
{
return stockRepository.ReadAll();
}
in efficient query executed
SELECT [c].[Id], [c].[created], [c].[isin], [c].[symbol]
FROM [company] AS [c]
本文标签:
版权声明:本文标题:c# - GraphQL, .NET Core, and Entity Framework Core create inefficient queries against database for DTOs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744284418a2598812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论