加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为何实体框架核心急于加载未包含的相关实体

发布时间:2020-12-15 07:50:37 所属栏目:百科 来源:网络整理
导读:我正在创建ASP.NET Core Web API,并且在查询上下文时遇到问题. Web API连接到具有3个表的数据库:雇员,事件和事件责任.员工可以创建许多事件.此外,可以为许多员工在每个事件中分配许多事件的责任(因此链接了事件责任表). 员工表 +----+-----------------+| I

我正在创建ASP.NET Core Web API,并且在查询上下文时遇到问题.

Web API连接到具有3个表的数据库:雇员,事件和事件责任.员工可以创建许多事件.此外,可以为许多员工在每个事件中分配许多事件的责任(因此链接了事件责任表).

员工表

+----+-----------------+
| Id |      Name       |
+----+-----------------+
|  1 | First Employee  |
|  2 | Second Employee |
+----+-----------------+

活动表

+----+------------+---------------+
| Id | Creator_Id |  Event_Name   |
+----+------------+---------------+
|  1 |          1 | Random Event  |
|  2 |          1 | Another Event |
+----+------------+---------------+

活动责任表

+----+-------------+----------+
| Id | Employee_Id | Event_Id |
+----+-------------+----------+
|  1 |           1 |        1 |
+----+-------------+----------+

这些模型包括…

事件模型

活动只能有一名雇员作为创建者.
一个事件也可以有许多承担不同职责的员工.

public int Id { get; set; }
public int? CreatorId { get; set; }
public string EventName { get; set; }

public Employee Creator { get; set; }
public ICollection<EventResponsibilities> EventResponsibilities { get; set; }

员工模式

员工可以成为许多事件的创建者.
员工可以在许多事件中承担责任.

public int Id { get; set; }
public string Name { get; set; }

public ICollection<Event> Event { get; set; }
public ICollection<EventResponsibilities> EventResponsibilities { get; set; }

事件_责任模型

多对多链接表模型.

public int Id { get; set; }
public int? EmployeeId { get; set; }
public int? EventId { get; set; }

public Employee Employee { get; set; }
public Event Event { get; set; }

我已经在控制器方法中运行以下LINQ查询…

return _context.Event.Include(e => e.EventResponsibilities).ThenInclude(e => e.Employee);

我希望得到一个JSON,其中包含事件的详细信息,其对应的事件职责以及与该职责相关的Employee.

我实际上得到的是以下json …

[
  {
    "Id": 1,"CreatorId": 1,"EventName": "Random Event","Creator": {
      "Id": 1,"Name": "First Employee","Event": [],"EventResponsibilities": [
        {
          "Id": 1,"EmployeeId": 1,"EventId": 1
        }
      ]
    },"EventResponsibilities": [
      {
        "Id": 1,"EventId": 1,"Employee": {
          "Id": 1,"EventResponsibilities": []
        }
      }
    ]
  },{
    "Id": 2,"EventName": "Another Event","Event": [
        {
          "Id": 1,"EventResponsibilities": [
            {
              "Id": 1,"EventId": 1
            }
          ]
        }
      ],"Event": {
            "Id": 1,"EventResponsibilities": []
          }
        }
      ]
    },"EventResponsibilities": []
  }
]

这不是我想要或期望的…
如您所见,由于某种原因,即使我没有在查询中请求它,也未显示Event(事件)模型的Creator导航属性,因为我没有使用Include().

此外,如果您查看JSON中第二个返回的事件(“ Id”:2),即使没有EventResponsibilities,也将返回两个创建者行.我不认为这是一个延迟加载问题,因为导航属性不是虚拟的.

我本来希望Creator对象为null或为空,因为我没有将其包含在内.

所以我的问题是:为什么即使我没有加载/包含Creator导航属性也是如此?另外,我也想知道

包括相关的创建者数据,并且仅在JSON中显示事件责任数据.

最佳答案
如果您查看documentation,则有一个具体说明实际上就是您陷入的陷阱:

Entity Framework Core will automatically fix-up navigation properties to any other entities that were previously loaded into the context instance. So even if you don’t explicitly include the data for a navigation property,the property may still be populated if some or all of the related entities were previously loaded.

通过做

.ThenInclude(e => e.Employee);

您请求EF提取此Employee,从而使它被急切地加载到它所引用的任何位置.并且由于在这种特定情况下,您的Event.Creator与您显式请求的EventResponsibilities.Employee相同,因此在将其提取到EventResponsibilities中之后,它也将提取到您的Event中.
如果您的Event.Creator是与EeventsResponsbilities.Employee不同的Employee,您将不会遇到这种异常情况.

这种意外行为和冗余数据是人们使用单独的DTO从控制器返回数据以确保API返回所需数量的正确数据的原因之一.

作为一种快速的解决方法,您只需在Creator属性上添加JsonIgnoreAttribute即可:

[JsonIgnore]
public Employee Creator { get; set; }

但是我个人不认为在同一类中混合DTO和Entity是一个很好的主意,因为每当任何实体发生变化时,都会引起各种麻烦.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读