Entity Framework Tutorial Basics(10):Entity Lifecycle
Entity Lifecycle:
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.
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.
Entity Framework Tutorial Basics(10):Entity Lifecycle的更多相关文章
- Entity Framework Tutorial Basics(3):Entity Framework Architecture
Entity Framework Architecture The following figure shows the overall architecture of the Entity Fram ...
- Entity Framework Tutorial Basics(9):Entity Relationships
这篇前面已经转载出来了,地址:http://www.cnblogs.com/purplefox2008/p/5646466.html
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- Entity Framework Tutorial Basics(31):Migration from EF 4.X
Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0 To migrate your existing Entity ...
- Entity Framework Tutorial Basics(4):Setup Entity Framework Environment
Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- Entity Framework Tutorial Basics(42):Colored Entity
Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
随机推荐
- loj #161 子集卷积
求不相交集合并卷积 sol: 集合并卷积?看我 FWT! 交一发,10 以上的全 T 了 然后经过参考别人代码认真比对后发现我代码里有这么一句话: rep(s, , MAXSTATE) rep(i, ...
- nodejs 不同请求获取前端传的参数
get方法 参数在req.query中获取 router.get('/', function(req, res, next) { console.log("reqquery:",r ...
- 扒站工具Teleport Pro教程
1.下载软件 http://www.jb51.net/softs/44134.html 2.安装 3.界面 先点开帮助点注册(类似于破解要不全站扒不全) 下面请看ppt, http://www.doc ...
- http协议及原理分析 1
1:200与304的区别 浏览器第一次加载成功返回200状态,并会在浏览器的缓存中记录下 max-age 这个值.第二次发起服务器的访问时 会先看缓存中有没有要加载的资源 如果有 再去看有没有超出 m ...
- XSS与CSRF攻击
一.XSS Cross Site Script,跨站脚本攻击.是指攻击者在网站上注入恶意客户端代码,通过恶意脚本对客户端网页进行篡改,从而在用户浏览网页时,对用户浏览器进行控制或者获取用户隐私数据的一 ...
- VirtualBox为虚拟OS硬盘扩容
1.关闭虚拟OS. 2.进入到在VirtualBox的安装路径,执行命令例子如: VBoxManage.exe modifyhd F:\VM\Debian7.2.vdi --resize 40000 ...
- HDU 2544 最短路(邻接表+优先队列+dijstra优化模版)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- mjpg-streamer在Ubuntu下编译,运行
1.将USB摄像头插上,查看是否找到设备,输入: wp@ubuntu:/home/$ ls /dev/video* /dev/video0 2.安装必要的软件集: sudo apt-get ...
- RabbitMQ 消息队列 应用
安装参考 详细介绍 学习参考 RabbitMQ 消息队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. M ...
- Android 4学习(6):概述 - 深入了解Android Activity
参考:<Professional Android 4 Application Development> 深入了解Android Activity 每一个Android Activity都对 ...