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. OpenCV - win7+vs2013(2012)+opencv3.0.0 环境配置 (以及配置技巧)

    1. opencv 3.0.0 库下载地址, 这里的版本是3.0.0,其他的版本配置可能不一样,请大家注意. http://sourceforge.net/projects/opencvlibrary ...

  2. flash、js 函数 互相调用

    js调用flex函数 flex 利用ExternalInterface.addCallback(“注册的方法名”,As中的函数名)进行注册 js中,用document.getElementById(“ ...

  3. webpack 故障处理

    Webpack 的配置比较复杂,很容出现错误,下面是一些通常的故障处理手段. 一般情况下,webpack 如果出问题,会打印一些简单的错误信息,比如模块没有找到.我们还可以通过参数 --display ...

  4. 1、Monkey入门准备教程

    1.前提需要Android环境 ADT:链接: https://pan.baidu.com/s/1QN6EJh46cJGvUBaMZjtiWw 密码: a7zu Eclipse:https://www ...

  5. 在Linux上利用core dump和GDB调试segfault

    时常会遇到段错误(segfault),调试非常费劲,除了单元测试和基本测试外,有些时候是在在线环境下,没有基本开发和测试工具,这就需要调试的技能.以前介绍过使用strace进行系统调试和追踪<l ...

  6. 【HTML代码】访问页面时,拨打页面中的电话号码

      这两天在写HTML页面今天要实现一个手机用户访问页面的时候,如果页面中有电话号码,点击可以打电话或者是发信息,之前没做过今天刚听说感觉很高大上会很难,实际上简单的让我内牛满面啊,,  小婊砸 (* ...

  7. echo 的部分用法

    echo “内容” > 文件名 (会覆盖文件里的所有内容) echo “内容” >> 文件名 (追加内容到文件里,会另起一行写入) 如果您阅读过此文章有所收获,请为我顶一个,如果文章 ...

  8. springboot springcloud eureka

    参考: https://www.cnblogs.com/skyblog/p/5133752.htmlhttp://blog.csdn.net/u012734441/article/details/78 ...

  9. HTML5的local storage

    function clickLogin(){ var userName = $("#user").val().trim(); // console.log(userName); / ...

  10. 2016.5.30实现透明Panel及控件置顶的方法

    想放置一个透明Panel在某控件上端,实现效果是可透过此Panel看见下面控件,但鼠标点击却无任何反应. 1.新建置自定义Panel类 using System; using System.Colle ...