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. hihocoder #1138 : Islands Travel

    题意,求1到n的最短路.不难想到单源最短路,难点在于数量级太大,因此如何建图是关键: 因为cost = min{|Xi-Xj|, |Yi-Yj|}:所以,点i的移动只有两种情况,. x距离最近的点,. ...

  2. php实现word在线浏览功能。

    http://laoniangke.com/php/2012/10/08/php-doc-webview.html

  3. mvc那些事

    mvc的特点: 1.无控件,有HtmlHelper类,此类提供了各种生成html控件的方法.如果不能满足需要,就自定义扩展吧,比如说分页显示.HtmlHelper类提供了Partial(加载局部视图) ...

  4. C#进阶之路(一):委托

    一.什么是委托 简单说它就是一个能把方法当参数传递的对象,而且还知道怎么调用这个方法,同时也是粒度更小的“接口”(约束了指向方法的签名). 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方 ...

  5. python模型字段

    http://python.usyiyi.cn/translate/django_182/ref/models/fields.html#model-field-types

  6. HttpContext是干什么的

    这是MSDN对HttpContext的说明:        HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息. (网上说是上下文信息,啥又叫上下文呢?个人感觉说的不 ...

  7. BZOJ1370:[Baltic2003]团伙

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...

  8. svn-clearup 报错的处理(Cleanup failed to process the following paths...)

    在使用 svn 客户端执行操作失败后,执行 Clean up 操作也报错:Cleanup failed to process the following paths... ,一直不知道是什么原因.通常 ...

  9. 第十五章 深入分析iBatis框架之系统架构与映射原理(待续)

    iBatis框架主要的类层次结构 iBatis框架的设计策略 iBatis框架的运行原理 iBatis框架对SQL语句的解析 数据库字段映射到Java对象 示例运行的结果 设计模式解析之简单工厂模式 ...

  10. DDD学习笔录——简介DDD的战略模式如何塑造应用程序的架构

    前一篇,简单介绍了DDD战略模式的提炼问题域,这篇简单介绍它如何塑造应用程序的架构. 1.创建一个模型以解决领域问题 为每一个子域构建一个软件模型以处理领域问题并让软件与业务保持一致. 这个模型并非现 ...