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. BackBone 源码解读及思考

    说明 前段时间略忙,终于找到时间看看backbone代码. 正如知友们说的那样,backbone简单.随性. 代码简单的看一眼,就能知道作者的思路.因为简单,所以随性,可以很自由的和其他类库大搭配使用 ...

  2. 数据处理之pandas库

    1. Series对象 由于series对象很简单,跟数组类似,但多了一些额外的功能,偷个懒,用思维导图表示 2. DaraFrame对象 DataFrame将Series的使用场景由一维扩展到多维, ...

  3. bzoj1249: SGU277 HERO 动态凸包

    动态维护凸包面积. //Achen #include<bits/stdc++.h> #define For(i,a,b) for(int i=(a);i<=(b);i++) #def ...

  4. 数据库使用JDBC连接的方式

    下面罗列了各种数据库使用JDBC连接的方式,可以作为一个手册使用. 1.Oracle8/8i/9i/10g/11g数据库(thin模式) Class.forName("oracle.jdbc ...

  5. java 实现插入排序

    sorted数组第0个位置没有放数据 从sorted数组的第二个数据开始处理 package com.learn; public class InsertionSort { public static ...

  6. plsql Developer----plsql软件总结(待续更新)

    如何查看数据库所有的表 打开pl/sql Developer连接上数据库了以后,在菜单 Tools 下面有个 Object browser 将其打勾(如果已经打勾了就不用管了),之后在IDE的左边有个 ...

  7. Log4net系统日志

    首先:引用Log4net.dll,按照说明进行web.config配置 然后:在Global中写入: protected void Application_Start(object sender, E ...

  8. Install boot2docker defaule username and password

    username passowrd method docker tcuser ssh root   command sudo -i

  9. DVWA平台v1.9-Brute Force

    Low: 随便输一下用户名,密码,test 点击Login 显示用户名或密码错误 在owasp-zap查看数据包 点击,就会转到这 右键,点击Fuzz 点击Remove删除默认的 选定参数变量值,点击 ...

  10. PHP类(二)-类的构造方法和析构方法

    构造方法 构造方法是对象创建完成后第一个被对象自动调用的方法,用来完成对象的初始化 在每个类中都会有一个构造方法,如果没有声明的话,类中会存在一个没有参数列表并且内容为空的构造方法.如果声明的话,默认 ...