ObjectStateManager 和分离实体状态

发布于 2024-12-15 16:26:16 字数 239 浏览 1 评论 0原文

我正在使用实体框架,但它的某些功能对我来说仍然不清楚。主要比较难理解的是:

  • SubmitChanges流程;
  • 调用更新-删除-插入方法的顺序;
  • 了解ObjectStateManager
  • 分离的EntityState及其提交过程的一部分;

提供您的解释或提供一些有用的链接。

I'm using Entity Framework and some parts of its functionality are still unclear for me. The main things that are hard to understand are:

  • SubmitChanges process;
  • Order of invoking update-delete-insert methods;
  • Understanding of ObjectStateManager
  • Detached EntityState and it's part in submitting process;

Provide your explanations or give some useful links.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2024-12-22 16:26:16

ObjectStateManager 是一个公开有关被跟踪实体的信息的组件。 EF 可以持久化的每个实体都必须被跟踪 = 附加。分离的实体对于 EF 来说是未知的(它们不会被跟踪),因此当您调用 SaveChanges 时,不会保存它们的更改(EF 中没有 SubmitChanges)。跟踪包括维护有关实体或关系的初始状态以及对它们所做的更改的信息。它还包含实体的全局状态。

调用 SaveChanges 时的过程取决于您配置 EF 跟踪更改的方式、获取实体的方式以及您所做的更改。

更改跟踪是一项功能,允许 EF 跟踪附加到上下文的实体上应用的更改(默认情况下附加查询加载的每个实体)。 EF 包含更改跟踪的版本:

  • 快照 - 仅适用于 POCO。 EF 不会跟踪实体的更改,但当您调用 SaveChanges 时,它会比较实体的存储状态(在加载实体时获取)和实体中的当前数据,并相应地设置其状态。
  • 动态 - 基于 EntityObject 的实体本机并通过 POCO 的动态代理实现。对附加实体的每次更改都会触发跟踪条目的更改并相应地设置实体状态。当您调用 SaveChanges 时,状态已设置。

数据修改操作的顺序是EF内部实现的。基本顺序由描述实体之间依赖关系的映射定义。

ObjectStateManager is a component exposing information about tracked entities. Each entity which can be persisted by EF must be tracked = attached. Detached entities are unknown to EF (they are not tracked) and so their changes are not saved when you call SaveChanges (there is no SubmitChanges in EF). Tracking consists of maintaining information about initial state of the entity or relation and changes done to them. It also contains the global state of the entity.

The process when you call SaveChanges depends on the way how you configured EF to track changes, on the way how you got the entity and on the changes you did.

Change tracking is a feature which allows EF to track changes applied on entities attached to the context (each entity loaded by the query is by default attached). EF contains to version of change tracking:

  • Snapshot - only for POCOs. EF doesn't track changes to entities but when you call SaveChanges it compares stored state of the entity (obtained when the entity was loaded) and the current data in the entity and set its state accordingly.
  • Dynamic - native for EntityObject based entities and achieved by dynamic proxies for POCOs. Each change to attached entity triggers change in tracked entry and sets entity state accordingly. When you call SaveChanges are states are already set up.

The order of data modification operations is EF internal implementation. The basic order is defined by your mapping where dependency between entities is described.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文