【问题标题】:Entity Framework + LINQ request实体框架 + LINQ 请求
【发布时间】:2020-05-03 15:46:44
【问题描述】:

我是 Entity Framework 和 Linq 的新手,我想了解在执行以下类型的 LINQ 请求时 dbset/dbcontext 是如何工作的:

from x in db.Products select x

db 是数据上下文对象,Products 是数据集。

这些记录的搜索/加载是直接在基表级别完成的,还是在数据库集上完成的第一次搜索?然后我们完成并检索数据库集中尚未跟踪的记录?

也就是说这些元素的加载路径是什么?

谢谢,

【问题讨论】:

    标签: c#


    【解决方案1】:

    Is the search/loading of these records done directly at the base table level or is a first search done on the dbset?

    通过直接从查询接收结果,在数据库服务器的“基表级别”完成此操作,然后 DataContext 将返回它已经跟踪的任何现有实体,如果没有任何实体,则创建新实体跟踪这些记录。

    And then we complete and retrieve the records that are not yet tracked/in the dbset?

    DataContext 将为这些记录创建新实体,如果您没有明确指定 AsNoTracking,它将被跟踪。

    In other words what is the path of the loading of these elements?

    this document 的工作方式是,当您进行这样的 LINQ 查询时:

    from x in db.Products select x

    它将生成一个 LINQ 表达式,然后将该表达式传递给数据库提供程序,以生成特定于它所针对的数据库引擎的实际数据库查询(它可能没有编译所有查询,因此某些查询可能是从应用程序端计算。)

    然后它将执行查询并接收来自该查询的结果,如果查询是通过跟踪进行的,那么它将返回 DataContext 已经跟踪的任何现有实体,如果没有,则创建新实体。

    实体和记录将通过密钥绑定,并且无论何时there is any part of the query using Keyless Entity Type, the whole query would be made as a NoTracking query.

    请注意,如果此类现有实体的数据库记录已更改,则不会更新现有实体中的值,您必须像这样手动重新加载该实体:

    db.Entry(product).ReloadAsync();

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 2013-04-05
      • 2020-06-10
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2013-07-29
      相关资源
      最近更新 更多