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 ...
随机推荐
- 4.MySQL优化---多表查询优化
整理自互联网 一.多表查询连接的选择: 相信这内连接,左连接什么的大家都比较熟悉了,当然还有左外连接什么的,基本用不上我就不贴出来了.这图只是让大家回忆一下,各种连接查询. 然后要告诉大家的是,需要 ...
- mysql前缀索引的应用
在mysql中有时需要索引的列很长,如果直接应用索引会造成索引过大的问题.因此我们可以取其中一部分字段来做索引,例: 添加索引:alter table * add key (field(3)); ...
- memcache内存分配问题
Memcached是一个高效的分布式内存cache,了解memcached的内存管理机制,便于我们理解memcached,让我们可以针对我们数据特点进行调优,让其更好的为我所用.这里简单谈一下我对me ...
- Chrome MarkDown Preview Plus
/************************************************************************** * Chrome MarkDown Previe ...
- CodeForces - 682E: Alyona and Triangles(旋转卡壳求最大三角形)
You are given n points with integer coordinates on the plane. Points are given in a way such that th ...
- Linux-CentOS7 安装VMware Workstation 12
转自:http://blog.csdn.net/aoshilang2249/article/details/48656107 1.下载VMware 衔接地址 http://www.vmware.com ...
- mysql 自增id
在开发的时候遇到了 自增id变成2147483647 莫名其妙 然后发现是自己没把自增id改为 无符号的原因 把无符号勾上就ok了
- Python:格式化操作符(%)
原文作者:田小计划 原文出处:http://www.cnblogs.com/wilber2013/ (若转载,请标明原文出处) 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整 ...
- Solaris10如何确认DirectIO是否已经启用
对于Oracle而言,如果数据库存储在UFS文件系统上,启用DirectIO能够提高数据库性能.Oracle有个参数filesystemio_options可以控制数据库是否使用DirectIO. ...
- AudioManager 音量『转』
获取系统音量 通过程序获取android系统手机的铃声和音量.同样,设置铃声和音量的方法也很简单! 设置音量的方法也很简单,AudioManager提供了方法: publicvoidsetStream ...