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 ...
随机推荐
- uva10943(隔板法)
很裸的隔板法. 引用一下维基上对隔板法的解释: 现在有10个球,要放进3个盒子里 ●●●●●●●●●● 隔2个板子,把10个球被隔开成3个部份 ●|●|●●●●●●●●.●|●●|●●●●●●●.●| ...
- 观后感|当幸福来敲门 The Pursuit of Happyness
更好的阅读体验请点击:当幸福来敲门 The Pursuit of Happyness 看到时光机点亮的那一刻,我想儿子克里斯托夫正在侏罗纪的世界内探险,看着山川河流,穿梭在恐龙的脚下,在山洞中安稳的度 ...
- Communication System(动态规划)
个人心得:百度推荐的简单DP题,自己做了下发现真得水,看了题解发现他们的思维真得比我好太多太多, 这是一段漫长的锻炼路呀. 关于这道题,我最开始用DP的思路,找子状态,发现自己根本就不会找DP状态数组 ...
- QAbstractSocket::connectToHost() called when already looking up or connecting/connected to
tcpSocket_connect_HBJ->abort();//取消已有连接,重置套接字,tcpSocket_connect_HBJ是QTcpSocket类的对象 就不会报错了.
- flask之python3 虚拟环境及使用dotnv来永久保存环境变量
Python 3 comes bundled with the venv module to create virtual environments Create an environment Cre ...
- springboot+springcloud config
参考:sorry,全找不到了,当时没记录,最后后知后觉觉得应该记录,所以后面的都有在asfood父项目中的doc文件夹下记录,望见谅. 1. springconfig server 1.1. pom. ...
- WebDriver测试web中遇到的弹出框或不确定的页面
我自己是用try catch解决的,不知道其他人的解决方法?如有,可以留言
- zedgraph控件的一些比较有用的属性
(1)zedgraph控件属性具体解释: AxisChange()() ->> This performs an axis change command on the graphPane. ...
- jsonp实现跨域请求的本质demo[无法发送post请求]
views.py def get_data(request): return HttpResponse("机密数据") urls.py urlpatterns = [ url(r' ...
- ASP.NET——配置文件——连接字符串
一.连接字符串 1.通过<connectionStrings>方式: 方式一:SqlServer身份验证 <connectionStrings> <add name=&q ...