0

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?

cenk takas
  • 23
  • 3

1 Answers1

0

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.

Chen
  • 4,499
  • 1
  • 2
  • 9