AspNet Core Api Ef Core是否带有额外的相关表格?

huangapple go评论61阅读模式
英文:

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&lt;IResponse&lt;Seller&gt;&gt; GetByIdAsyncR(int id)
        {
            var data = await _context.Sellers.Where(i =&gt; i.Id == id)
                .Include(i =&gt; i.Categories)
                .Include(i =&gt; i.AppUsers)
                .Include(i =&gt; i.Products)
                .Include(i =&gt; i.Orders)
                .FirstOrDefaultAsync();
            if (data != null)
                return new Response&lt;Seller&gt;(ResponseType.Success, data);
            return new Response&lt;Seller&gt;(ResponseType.NotFound, &quot;Data bulunamadı&quot;);
        }

This is the result

{
  &quot;email&quot;: &quot;a1@a.com&quot;,
  &quot;offers&quot;: [],
  &quot;categories&quot;: [
    
      &quot;name&quot;: &quot;cat1&quot;,
      &quot;description&quot;: &quot;des1&quot;,
      &quot;language&quot;: 0,
      &quot;sellerId&quot;: 1,
      &quot;imageId&quot;: null,
      &quot;image&quot;: null,
      &quot;products&quot;: [
        {
          &quot;name&quot;: &quot;pd 1&quot;,
          &quot;language&quot;: 0,
          &quot;description&quot;: &quot;desdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdesdes1&quot;,
          &quot;isStock&quot;: true,
          &quot;isNew&quot;: true,
          &quot;sellerId&quot;: 1,
          &quot;categoryId&quot;: 1,
          &quot;orderItems&quot;: [],
          &quot;images&quot;: [],
          &quot;cardItems&quot;: [],
          &quot;menuProduct&quot;: [],
          &quot;id&quot;: 1,
          &quot;createdDate&quot;: &quot;2023-02-13T01:52:17.5190827&quot;
        }
      ],
      &quot;id&quot;: 1,
      &quot;createdDate&quot;: &quot;2023-02-13T01:52:17.5190795&quot;
    }

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 =&gt; i.Id == id)
           .Include(i =&gt; i.Categories)
           .Include(i =&gt; i.AppUsers)
           .Include(i =&gt; i.Products)
           .Include(i =&gt; 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 =&gt; i.Id == id)
          .Select(x =&gt; new {
                ParentRecord = x,
                ChildRecord = x.Categories,
                HasChildRecords = x.Categories.Any()
          })
          .Include(i =&gt; i.AppUsers)
          .Include(i =&gt; i.Products)
          .Include(i =&gt; i.Orders)
          .FirstOrDefaultAsync();

Hope this can help you.

huangapple
  • 本文由 发表于 2023年2月14日 00:59:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定