Change Tracking in Entity Framework:

Here, you will learn how entity framework tracks changes on entities during its life time.

Entity framework supports automatic change tracking of the loaded entities during the life time of the context. DbChangeTracker class gives you all the information about current entities being tracked by the context.

Please note that every entity must have EntityKey (primary key) property in order to be tracked by the context. Entity framework will not add any entity in the conceptual model which does not have an EntityKey property.

The following code snippet shows how context class tracks the entities and changes occurred in it:

static void Main(string[] args)
{
using (var ctx = new SchoolDBEntities())
{ Console.WriteLine("Find Student");
var std1 = ctx.Students.Find(); Console.WriteLine("Context tracking changes of {0} entity.", ctx.ChangeTracker.Entries().Count()); DisplayTrackedEntities(ctx.ChangeTracker); Console.WriteLine("Find Standard"); var standard = ctx.Standards.Find(); Console.WriteLine("Context tracking changes of {0} entities.", ctx.ChangeTracker.Entries().Count());
Console.WriteLine("");
Console.WriteLine("Editing Standard"); standard.StandardName = "Edited name";
DisplayTrackedEntities(ctx.ChangeTracker); Teacher tchr = new Teacher() { TeacherName = "new teacher" };
Console.WriteLine("Adding New Teacher"); ctx.Teachers.Add(tchr);
Console.WriteLine("");
Console.WriteLine("Context tracking changes of {0} entities.", ctx.ChangeTracker.Entries().Count());
DisplayTrackedEntities(ctx.ChangeTracker); Console.WriteLine("Remove Student");
Console.WriteLine(""); ctx.Students.Remove(std1);
DisplayTrackedEntities(ctx.ChangeTracker);
}
} private static void DisplayTrackedEntities(DbChangeTracker changeTracker)
{
Console.WriteLine(""); var entries = changeTracker.Entries();
foreach (var entry in entries)
{
Console.WriteLine("Entity Name: {0}", entry.Entity.GetType().FullName);
Console.WriteLine("Status: {0}", entry.State);
}
Console.WriteLine("");
Console.WriteLine("---------------------------------------");
}
Output:

Find Student 
Context tracking changes of 1 entity.

Entity Name: EFTutorials.Student
Status: Unchanged

---------------------------------------
Find Standard
Context tracking changes of 2 entities.

Editing Standard

Entity Name: EFTutorials.Standard
Status: Modified
Entity Name: EFTutorials.Student
Status: Unchanged

---------------------------------------
Adding New Teacher

Context tracking changes of 3 entities.

Entity Name: EFTutorials.Teacher
Status: Added
Entity Name: EFTutorials.Standard
Status: Modified
Entity Name: EFTutorials.Student
Status: Unchanged

---------------------------------------
Remove Student

Entity Name: EFTutorials.Teacher
Status: Added
Entity Name: EFTutorials.Standard
Status: Modified
Entity Name: EFTutorials.Student
Status: Deleted

---------------------------------------

As you can see in the above sample code snippet and output, context keeps track of entities whenever we retrieve, add, modify or delete any entity. Please notice that context is alive during any of the operations on entities. Context will not keep track if you do any operation on entities out of its scope.

Entity Framework Tutorial Basics(19):Change Tracking的更多相关文章

  1. Entity Framework Tutorial Basics(2):What is Entity Framework?

    What is Entity Framework? Writing and managing ADO.Net code for data access is a tedious and monoton ...

  2. Entity Framework Tutorial Basics(1):Introduction

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

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

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

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

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

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

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

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

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

  8. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...

  9. Entity Framework Tutorial Basics(34):Table-Valued Function

    Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...

随机推荐

  1. MySQL相关日志介绍

    1.MySQL中主要日志如下: ① 错误日志(Log Error) ② 查询日志(Query Log) ③ 二进制日志(Binary Log) 2.相关日志的作用: 1) 错误日志(Error Log ...

  2. Cash Machine(多重背包二进制转换)

    个人心得:多重背包,自己根据转换方程写总是TLE,后面去网上看了二进制转换,不太理解: 后面仔细想了下,用自己的思想理解下把,就是将对应number,cash总和用二进制拆分, 然后全部装入到一个数组 ...

  3. Knuth-Morris-Pratt 算法

    KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt同时发现,因此人们称它为克努特——莫里斯——普拉特操作(简称KMP算法).KMP算法的关键是利用匹配 ...

  4. 基于JDK1.7.0_80与JDK1.8.0_66做的分析

    JDK1.7中 使用一个Entry数组来存储数据,用key的hashcode取模来决定key会被放到数组里的位置,如果hashcode相同,或者hashcode取模后的结果相同(hash collis ...

  5. wpf控件提示Value ‘’ can not convert

    我们在对控件的ErrorTemplate设置后,有时会出现Value '' can not convert. 为什么会出现呢? 原因:如果控件的输入值和null不能转换(比如控件要求的是int或flo ...

  6. HTML5离线应用

    本地缓存与浏览器缓存 本地缓存是为整个web应用程序服务的而网页缓存值服务与单个网页 本地缓存是为你指定的资源进行缓存,而我们不知道网页缓存会春初哪些内容,他是不安全不可靠的 在没有网络的时候还是可以 ...

  7. 学习Linux相关书籍

    要推荐的书,我在<那两年炼就的Android内功修养>这篇文章中有提到,这里再列一下出来: 语言类: <深度探索C++对象模型>,对应的英文版是<Inside C+++  ...

  8. dialog插件demo

    基本操作 默认窗体 new Dialog('这是一个默认对话框').show(); 非模态对话框 new Dialog('非模态对话框,可以打开多个!',{modal:false}).show(); ...

  9. 三种web性能压力测试工具

    三种web性能压力测试工具http_load webbench ab小结 题记:压力和性能测试工具很多,下文讨论的是我觉得比较容易上手,用的比较多的三种 http_load 下载地址:http://w ...

  10. mac 下 配置appium +ios真机环境

    mac系统:10.11.6 xcode:7 appium:1.5.3 iphone: 6 p 1.搭建 appium 安卓的环境: 1.jdk 2.sdk 3.appium 4.配置环境变量 mac下 ...