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 ...
随机推荐
- DNS分别在什么情况下使用UDP和TCP?
看到一个题目问的是:DNS分别在什么情况下使用UDP和TCP? 仔细查了一下发现可查询的资料很少,大部分都把DNS所用协议归为UDP了其实不然,DNS同时占用UDP和TCP端口53,这种单个应用协议同 ...
- bzoj 3681 Arietta
一棵有根树,每个点有一个音高,有 $m$ 中弹奏方法,每种方法可以弹奏 $d$ 子树中音高在 $[l,r]$ 间的音符,每种方法最多弹 $t$ 次 求最多能弹出多少个音符 $n \leq 10000$ ...
- Number Sequence (KMP的应用)
个人心得:朴素代码绝对超时,所以要用到KMP算法,特意了解了,还是比较抽象,要多体会 Given two sequences of numbers : a11, a22, ...... , aNN, ...
- LeetCode Can Place Flowers
原题链接在这里:https://leetcode.com/problems/can-place-flowers/description/ 题目: Suppose you have a long flo ...
- windowsError:32
Traceback (most recent call last): File "/usr/lib/python2.7/logging/handlers.py", line 78, ...
- 综述c++
1.背景 C语言作为结构化和模块化的语言,在处理较小规模的程序时,比较得心应手.但是当问题比较复杂,程序的规模较大时,需要高度的抽象和建模时,C语言显得力不从心. 2.应用领域 如果项目中,既要求效率 ...
- mybatis与oracle使用总结
Oracle使用总结 1.新建表删除表 新建表语句: CREATE TABLE +表名{ } create table AFA_USER ( USER_ID VARCHAR2() not null, ...
- hihoCoder1304:24点
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 周末,小Hi和小Ho都在家待着. 在收拾完房间时,小Ho偶然发现了一副扑克,于是两人考虑用这副扑克来打发时间. 小Ho:玩 ...
- websocket之三:Tomcat的WebSocket实现
Tomcat自7.0.5版本开始支持WebSocket,并且实现了Java WebSocket规范(JSR356 ),而在7.0.5版本之前(7.0.2版本之后)则采用自定义API,即WebSocke ...
- final,finally和finalize三者的区别和联系
对于初学者而言(当然也包括我)对于这三者真的不是很陌生,经常会看到它们.但对于三者之间的区别和联系一直是懵懵懂~~ 今天心情不错,那就简单总结一下它们几个的区别和联系吧.如果又不对的地方欢迎批评指正~ ...