实体生命周期【Entity Lifecycle】(EF基础系列10)
Before we work on CRUD operation (Create, Read, Update, Delete), it's important to understand the entity lifecycle and how it is being managed by the EntityFramework.
During an entity's lifetime, each entity has an entity state based on the operation performed on it via the context (DbContext). The entity state is an enum of type System.Data.Entity.EntityState that includes the following values:
- Added
- Deleted
- Modified
- Unchanged
- Detached
The Context not only holds the reference to all the objects retrieved from the database but also it holds the entity states and maintains modifications made to the properties of the entity. This feature is known as Change Tracking.
The change in entity state from the Unchanged to the Modified state is the only state that's automatically handled by the context. All other changes must be made explicitly using proper methods of DbContext and DbSet.
The following figure illustrates how the operation performed on entity changes its' states which, in turn, affects database operation.
在我们进行增删查改操作之前,相当重要去理解实体的生命周期,它是怎么被EF操作的。
在实体的生命周期过程中,每个实体基于上下文的操作都会有一个实体状态,实体状态是一个枚举类型的值:System.Data.Entity.EntityState,包含下面的值:
1.Added
2.Deleted
3.Modified
4.Unchanged
5.Detached
数据上下文不仅包含所有从数据库中检索的对象的引用,并且它有这个实体对象的实体状态,维护修改实体的属性,这个特性叫做更改跟踪。
实体状态从“Unchaged”到“Modified”的改变是由数据上下文自动处理的,所以其他状态的改变,必须要使用DbContext和DbSet合适的方法.
下面的图表,列出了,改变实体状态的操作,反过来是怎么影响数据库的操作的。

As you can see in the above figure, new entity in context has Added entity state. So the context will execute insert command to the database. In the same way, when you retrieve an existing entity using L2E queries, it will have Unchanged state, this is because you have just retrieved an entity and hasn't performed any operation on it yet. When you modify values of existing entity, it changes its state to Modified which in turn will execute update command on SaveChanges. Deleted entity from context will have Deleted state which in turn will execute delete command to the database.
So, in this way, operations performed on entities changes states. Context builds and executes database commands based on the state of an entity.
一个新的实体,保存的时候,生成的是Insert命令;
查询一个实体,实体的状态是Unchanged;
修改实体之后,再保存,就是Update;
删除就是Delete
实体生命周期【Entity Lifecycle】(EF基础系列10)的更多相关文章
- 【Basics of Entity Framework】【EF基础系列1】
EF自己包括看视频,看MSDN零零散散的学了一点皮毛,这次打算系统学习一下EF.我将会使用VS2012来学习这个EF基础系列. 现在看看EF的历史吧: EF版本 相关版本特性介绍 EF3.5 基于数据 ...
- 1.翻译:EF基础系列--什么是Entity Framework?
大家好,好久不见,EF系列之前落下了,还是打算重新整理一下. 先说说目前的打算:先简单了解一下EF基础系列-->然后就是EF 6 Code-First系列-->接着就是EF 6 DB-Fi ...
- 10.翻译:EF基础系列---EF中的持久性
原文链接:http://www.entityframeworktutorial.net/EntityFramework4.3/persistence-in-entity-framework.aspx ...
- Android 进程生命周期 Process Lifecycle
Android 进程生命周期 Process Lifecycle 进程的生命周期 Android系统会尽力保持应用的进程,但是有时为了给新的进程和更重要的进程回收一些内存空间,它会移除一些旧的进程. ...
- EntityFramework 学习 一 Entity Lifecycle 实体生命周期
当我们执行CRUD(Create,Read,Update,Delete)操作之前,最重要的是搞明白实体的生命周期和EntityFrameword怎么管理实体 在一个实体的生命周期里,每个实体都有一个实 ...
- EF中的实体类型【Types of Entity in Entity】(EF基础系列篇8)
We created EDM for existing database in the previous section. As you have learned in the previous se ...
- 8.翻译:EF基础系列----EF中实体的状态
原文链接:http://www.entityframeworktutorial.net/basics/entity-states.aspx 在实体的生命周期中,EF API维护着每一个实体的状态,对于 ...
- 7.翻译:EF基础系列---EF中的实体类型
原文地址:http://www.entityframeworktutorial.net/Types-of-Entities.aspx 在Entity Framework中有两种实体类型:一种是POCO ...
- 6.翻译:EF基础系列---什么是EF中的实体?
原文地址:http://www.entityframeworktutorial.net/basics/what-is-entity-in-entityframework.aspx EF中的实体就是继承 ...
随机推荐
- nginx 启动不了的小问题
nginx 配置的端口可能没打开 linux打开端口: /sbin/iptables -I INPUT -p tcp --dport 8011 -j ACCEPT #开启8011端口 /etc/ ...
- IOS网络第七天WebView-04仿网易新闻详情
*************** #import "HMViewController.h" @interface HMViewController () @end @implemen ...
- 一步一步学ROP之linux_x86篇
一步一步学ROP之linux_x86篇 作者:蒸米@阿里聚安全 一.序 ROP的全称为Return-oriented programming(返回导向编程),这是一种高级的内存攻击技术可以用来绕过 ...
- ABP理论学习之应用服务
返回总目录 本篇目录 IApplicationService接口 ApplicationService类 工作单元 数据库连接和事务管理 自动保存更改 更多 应用服务的生命周期 应用服务用于将领域逻辑 ...
- static与并发
在java中static用来修饰Class类中属性和方法. 被static修饰的成员属性和成员方法独立于该类的任何对象,它们在内存空间上会被放在描述Class的位置中,也就是说它们为此类(Class) ...
- [OpenGL][SharpGL]用Polygon Offset解决z-fighting和stitching问题
[OpenGL][SharpGL]用Polygon Offset解决z-fighting和stitching问题 本文参考了(http://www.zeuscmd.com/tutorials/open ...
- C语言 · 图形显示
问题描述 编写一个程序,首先输入一个整数,例如5,然后在屏幕上显示如下的图形(5表示行数): * * * * * * * * * * * * * * * #include "stdi ...
- MySQL_01之MySQL数据库基础
1.通过SQL(结构化查询语言)操作数据库: DDL:数据定义语言,创建库,创建表,选择: DML:数据操作语言,完成数据增删改: DQL:数据查询语言,完成数据查询: DCL:数据控制语言,授权.回 ...
- SVN代码冲突解决方案小集合
对于刚接触svn的人来说,svn冲突后,不能提交是件让人很郁闷的事情.最让人郁闷的事,是代码间的覆盖.你把我代码盖了,我会很火大的.谁把谁的盖了都不爽. 为什么会出现代码冲突问题呢,因为不同的人,同时 ...
- c#写windows服务
序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用Windows服务实现.这篇就总 ...