Entity Framework Tutorial Basics(19):Change Tracking
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("---------------------------------------");
}
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的更多相关文章
- 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 ...
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- 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 ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- 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 ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- 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 ...
- 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 ...
随机推荐
- 利用HTML5开发Android笔记(中篇)
资源来自于www.mhtml5.com 杨丰盛老师成都场的PPT分享 一个很简明的demo 可以作为入门基础 学习的过程中做了点笔记 整理如下 虽然内容比较简单 但是数量还是比较多的 所以分了3篇 ( ...
- 机器学习:YOLO for Object Detection (二)
之前介绍了 YOLO-v1 单纯的利用一个卷积网络完成了目标检测,不过 YOLO-v1 虽然速度很快,但是比起其他的网络比如 Fast R-CNN 检测的准确率还是差不少,所以作者又提出了改良版的 Y ...
- mysql出现mysql server has gone away错误的解决办法
应用程序(比如PHP)长时间的执行批量的MYSQL语句.执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段.比如,图片数据的处理.都容易引起MySQLserver has ...
- 教你用 Python 实现抖音热门表白软件
之前在群里看到有人发了一个抖音上很火的小视频,就是一个不正经的软件,运行后问你是不是愿意做我的朋友,但你没法点击到「不同意」!并且没办法直接关闭窗口! 很不正经,很流氓,有点适合我. 效果大概是这样的 ...
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- deque容器
一.deque容器基本概念 deque是“double-ended queue”的缩写,和vector一样,deque也支持随机存取.vector是单向开口的连续性空间,deque则是一种双向开口的连 ...
- 6、Selenium+Python登录案例 -- Github
一:登录 1.指定浏览器,打开网址:https://github.com/login 2.设置等待时间: time.sleep(3) or driver.implicitly_wait(3) 3.输入 ...
- JSF结合Spring 引入ViewScope
当JSF项目的faceConfig中配置了Spring的配置代码 <application> <el-resolver>org.springframework.web.jsf. ...
- 蓝桥杯 历届试题 PREV-32 分糖果
历届试题 分糖果 时间限制:1.0s 内存限制:256.0MB 问题描述 有n个小朋友围坐成一圈.老师给每个小朋友随机发偶数个糖果,然后进行下面的游戏: 每个小朋友都把自己的糖果分一半给左手边 ...
- java流类共享篇
总结: package com.aini; import java.io.*; import java.util.StringBuffere; public class tyt { public st ...