zoukankan      html  css  js  c++  java
  • 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(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.

  • 相关阅读:
    Delphi TcxComboBox控件说明
    Delphi ComboBox的属性和事件、及几个鼠标事件的触发
    Delphi XE5的新功能“ TListView内置搜索过滤”
    可能是全网最全的解决摄像头无法创建视频捕捉过滤器问题?
    Delphi – TDataSet确定它是否在插入/编辑状态时被修改
    Delphi 获取DataSet传入参数后的SQL命令
    TClientDataSet[7]: 辨析 Field、FieldDef、Fields、FieldDefs、FieldList、FieldDefList
    枚举HasFlag函数实现的内部逻辑是什么
    在DBGrid中用代码实现按回车键跳到下一格的方法
    eclipse下没有Dynamic Web Project的处理方法
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649047.html
Copyright © 2011-2022 走看看