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:

  1. Added
  2. Deleted
  3. Modified
  4. Unchanged
  5. 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的更多相关文章

  1. Entity Framework Tutorial Basics(3):Entity Framework Architecture

    Entity Framework Architecture The following figure shows the overall architecture of the Entity Fram ...

  2. Entity Framework Tutorial Basics(9):Entity Relationships

    这篇前面已经转载出来了,地址:http://www.cnblogs.com/purplefox2008/p/5646466.html

  3. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  4. 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 ...

  5. 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 ...

  6. Entity Framework Tutorial Basics(43):Download Sample Project

    Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...

  7. 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 ...

  8. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  9. Entity Framework Tutorial Basics(37):Lazy Loading

    Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...

随机推荐

  1. 在ios7中使用zxing

    ZXing(Github镜像地址)是一个开源的条码生成和扫描库(开源协议为Apache2.0).它不但支持众多的条码格式,而且有各种语言的实现版本,它支持的语言包括:Java, C++, C#, Ob ...

  2. LKDBHelper Sqlite操作数据库

    首先这里要说明一下,为什么用FMDB而不用Core Data呢,因为我们不知道Core Data是如何映射对象里面的属性关系的,如果我们更改了属性的话,就会报错 首先是创建LKDBHelper对象 L ...

  3. [Unity3D]EZGUI 操作简单介绍

    官方的GUI根本无法跟EZGUI比,无论是资源还是易用性还是速度.EZGUI基于Mesh不占DrawCall.EZGUI是自动合并Mesh成为一个物体,并且贴图自动制作Atlas.所以效率高,CPU消 ...

  4. grpc asp.net core 集成时一些配置的说明

    一  什么是grpc google出了一款分布式通讯框架:grpc.我想这也不是新的东西了,在13年的一个项目中,用在了数据层和业务端之间的通讯上,当时并没有觉得怎么样,因为wcf很轻松的也可以可以实 ...

  5. [独孤九剑]Oracle知识点梳理(九)数据库常用对象之package

    本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...

  6. Oracle job procedure 存储过程定时任务(转自hoojo)

    Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相关视图 ...

  7. Python函数-abs()

    说明: 返回绝对值 参数可以是:负数.正数.浮点数或者长整形 实例: abs(-1.2) #返回 1.2 abs(1.2) #返回 1.2 abs(-11216.5) #返回 11216.5 abs( ...

  8. 详解NXP Cortex-M3加密设置

     加密芯片是对内部集成了各类对称与非对称算法,自身具有极高安全等级,可以保证内部储存的密钥和信息数据不被非法读取与篡改的一类安全芯片的通称.使用到安全加密芯片的产品:银行加密U盾.刻录机.加密硬盘.P ...

  9. java事件练习!!

    总结:不晓得怎么跟书上的运行结果显示的...希望标签竖直排列 package com.bc; import java.awt.Color; import java.awt.FlowLayout; im ...

  10. java流。基础

    总结:read()方法返回类型是int型??不知道该怎么用好循环> package com.da; import java.io.*; public class fgbv { public st ...