博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Entity Framework Tutorial Basics(19):Change Tracking
阅读量:6903 次
发布时间:2019-06-27

本文共 3456 字,大约阅读时间需要 11 分钟。

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(1);        Console.WriteLine("Context tracking changes of {0} entity.", ctx.ChangeTracker.Entries().Count());        DisplayTrackedEntities(ctx.ChangeTracker);        Console.WriteLine("Find Standard");        var standard = ctx.Standards.Find(1);        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.

转载于:https://www.cnblogs.com/purplefox2008/p/5649047.html

你可能感兴趣的文章
DBI 数据库模块剖析:Perl DBI 数据库通讯模块规范,工作原理和实例
查看>>
Tesseract+opencv+VS+win实现OCR
查看>>
android在activity中锁屏解锁后重走OnCreate的问题的解决办法
查看>>
[学习笔记]博弈论
查看>>
python基础:搜索路径
查看>>
python os sys模块(二)
查看>>
一次linux启动故障记录
查看>>
linux 3.10内核 xfs的一次io异常导致的hung crash
查看>>
Castle ActiveRecord学习笔记(转)
查看>>
change textblock background color when text equal to referenceValue
查看>>
springboot+mybatis环境的坑和sql语句简化技巧
查看>>
如何用oracle从身份证信息中提取出生日期?
查看>>
Keil C编译器的变量存储分配
查看>>
非常不错的js 屏蔽类加验证类
查看>>
Innodb间隙锁,细节讲解(转)
查看>>
Apache安装
查看>>
C语言练习题库----数组
查看>>
算法的时间复杂度详解
查看>>
制作3D旋转视频展示区
查看>>
Spring.Net初认识——竹子整理
查看>>