英文:
AspNet Core Api Ef Core comes with extra related tables?
问题
当我在Ef Core中将Categories表包含在卖家表中时,它会同时返回Category中的Product,但我只想要Category表。数据出现了重载。我该如何解决这个问题?为什么在卖家下的Category中也出现了Products?我只写了.Include(Category),Products不应该出现吗?
我包含的函数如下:
public async Task<IResponse<Seller>> GetByIdAsyncR(int id)
{
var data = await _context.Sellers.Where(i => i.Id == id)
.Include(i => i.Categories)
.Include(i => i.AppUsers)
.Include(i => i.Products)
.Include(i => i.Orders)
.FirstOrDefaultAsync();
if (data != null)
return new Response<Seller>(ResponseType.Success, data);
return new Response<Seller>(ResponseType.NotFound, "Data bulunamadı");
}
这是结果:
{
"email": "a1@a.com",
"offers": [],
"categories": [
{
"name": "cat1",
"description": "des1",
"language": 0,
"sellerId": 1,
"imageId": null,
"image": null,
"products": [
{
"name": "pd 1",
"language": 0,
"description": "desdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdes1",
"isStock": true,
"isNew": true,
"sellerId": 1,
"categoryId": 1,
"orderItems": [],
"images": [],
"cardItems": [],
"menuProduct": [],
"id": 1,
"createdDate": "2023-02-13T01:52:17.5190827"
}
],
"id": 1,
"createdDate": "2023-02-13T01:52:17.5190795"
}
]
}
我不明白为什么Seller->Category->Product会显示出来?Products不应该在Category下为空吗?
我不明白为什么在Category下Products为空。它不应该是null吗?
英文:
When I include the Categories table in the seller table with Ef Core, it comes with Product in the Category, I just want the category table to come. There is an overload of data. How can i solve it. Why does Products also appear in Category under Seller only? I just wrote .Include(Category), Products should not come??
The function I included
public async Task<IResponse<Seller>> GetByIdAsyncR(int id)
{
var data = await _context.Sellers.Where(i => i.Id == id)
.Include(i => i.Categories)
.Include(i => i.AppUsers)
.Include(i => i.Products)
.Include(i => i.Orders)
.FirstOrDefaultAsync();
if (data != null)
return new Response<Seller>(ResponseType.Success, data);
return new Response<Seller>(ResponseType.NotFound, "Data bulunamadı");
}
This is the result
{
"email": "a1@a.com",
"offers": [],
"categories": [
"name": "cat1",
"description": "des1",
"language": 0,
"sellerId": 1,
"imageId": null,
"image": null,
"products": [
{
"name": "pd 1",
"language": 0,
"description": "desdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdes1",
"isStock": true,
"isNew": true,
"sellerId": 1,
"categoryId": 1,
"orderItems": [],
"images": [],
"cardItems": [],
"menuProduct": [],
"id": 1,
"createdDate": "2023-02-13T01:52:17.5190827"
}
],
"id": 1,
"createdDate": "2023-02-13T01:52:17.5190795"
}
I don't understand why Seller->Category->Product is coming?
Shouldn't the Products under Category be empty?
I couldn't understand why Products is empty under Category. Shouldn't it be null?
答案1
得分: 0
这是一个正常的查询结果。 Include() 告诉EF您想要获取子记录以进行处理。如果您不想要子实体,请不要使用 Include。
就像Svyatoslav Danyliv所说,您可以将 AsNoTracking 添加到您的查询中:
var data = await _context.Sellers.Where(i => i.Id == id)
.Include(i => i.Categories)
.Include(i => i.AppUsers)
.Include(i => i.Products)
.Include(i => i.Orders)
.AsNoTracking()
.FirstOrDefaultAsync();
AsNoTracking() 允许您告诉Entity Framework Core不要跟踪查询的结果。这意味着Entity Framework Core不对查询返回的实体执行任何额外的处理或存储。
如果您想使用 Select(),您可以参考以下代码以更直观地了解 Select 查询结果:
var data = await _context.Sellers.Where(i => i.Id == id)
.Select(x => new {
ParentRecord = x,
ChildRecord = x.Categories,
HasChildRecords = x.Categories.Any()
})
.Include(i => i.AppUsers)
.Include(i => i.Products)
.Include(i => i.Orders)
.FirstOrDefaultAsync();
希望这可以帮助您。
英文:
This is a normal query result. Include() tells EF that you want to pull child records for processing. If you don't want child entities, don't use Include.
Like as Svyatoslav Danyliv said, you can add AsNoTracking to your query:
var data = await _context.Sellers.Where(i => i.Id == id)
.Include(i => i.Categories)
.Include(i => i.AppUsers)
.Include(i => i.Products)
.Include(i => i.Orders)
.AsNoTracking()
.FirstOrDefaultAsync();
AsNoTracking() allows you to tell Entity Framework Core not to track the results of a query. This means that Entity Framework Core performs no additional processing or storage of the entities which are returned by the query.
If you want to use Select(), you can refer to the following code to feel the query results of Select more intuitively:
var data = await _context.Sellers.Where(i => i.Id == id)
.Select(x => new {
ParentRecord = x,
ChildRecord = x.Categories,
HasChildRecords = x.Categories.Any()
})
.Include(i => i.AppUsers)
.Include(i => i.Products)
.Include(i => i.Orders)
.FirstOrDefaultAsync();
Hope this can help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论