none
What is EF tracking and what it does RRS feed

  • Question

  • i am new in EF. so please tell me what is EF tracking and what it does ?

    also tell me what is the benefit of EF tracking ?

    please discuss the EF tracking with a nice example code. thanks

    Tuesday, February 27, 2018 9:33 AM

Answers

  • https://msdn.microsoft.com/en-us/library/jj613924(v=vs.113).aspx

    It doesn't work for Web solutions, because Web solutions are stateless, and EF is always in a disconnected state.  

    • Marked as answer by Sudip_inn Thursday, March 1, 2018 5:26 PM
    Tuesday, February 27, 2018 5:34 PM
  • Hi Sudip_inn,

    Entity Framework supports automatic change tracking of the loaded entities during the life-time of the context. The DbChangeTracker class gives you all the information about current entities being tracked by the context.

    Please note that every entity must have a key (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.

    Here is sample which shows how the context class tracks the entities and the changes occurred on them for your reference:

    class Program
        {
            static void Main(string[] args)
            {
                using (var ctx = new EFDemoContext())
                {
                    Console.WriteLine("Find Blog");
                    var blog1 = ctx.Blogs.Find(1);
                    Console.WriteLine("Context tracking changes of {0} entity.", ctx.ChangeTracker.Entries().Count());
                    DisplayTrackedEntities(ctx.ChangeTracker);
                    Console.WriteLine("Find Post");
                    var post = ctx.Posts.Find(1);
                    Console.WriteLine("Context tracking changes of {0} entities.", ctx.ChangeTracker.Entries().Count());
                    Console.WriteLine("Editing Posting");
                    post.Content = "Edited Content";
                    DisplayTrackedEntities(ctx.ChangeTracker);
                    Blog blog  = new Blog() { Name = "new Blog", Url = "#" };
                    Console.WriteLine("Adding New Post");
                    ctx.Blogs.Add(blog);
                    Console.WriteLine("Context tracking changes of {0} entities.", ctx.ChangeTracker.Entries().Count());
                    DisplayTrackedEntities(ctx.ChangeTracker);
                    Console.WriteLine("Remove Blog");
                    ctx.Blogs.Remove(blog1);
                    DisplayTrackedEntities(ctx.ChangeTracker);
                    Console.ReadKey();
                }
            }
    
            private static void DisplayTrackedEntities(DbChangeTracker changeTracker)
            {
                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("---------------------------------------");
            }
        }

    Best regards,

    Zhanlgong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, March 1, 2018 5:26 PM
    Wednesday, February 28, 2018 1:24 AM
    Moderator

All replies

  • https://msdn.microsoft.com/en-us/library/jj613924(v=vs.113).aspx

    It doesn't work for Web solutions, because Web solutions are stateless, and EF is always in a disconnected state.  

    • Marked as answer by Sudip_inn Thursday, March 1, 2018 5:26 PM
    Tuesday, February 27, 2018 5:34 PM
  • Hi Sudip_inn,

    Entity Framework supports automatic change tracking of the loaded entities during the life-time of the context. The DbChangeTracker class gives you all the information about current entities being tracked by the context.

    Please note that every entity must have a key (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.

    Here is sample which shows how the context class tracks the entities and the changes occurred on them for your reference:

    class Program
        {
            static void Main(string[] args)
            {
                using (var ctx = new EFDemoContext())
                {
                    Console.WriteLine("Find Blog");
                    var blog1 = ctx.Blogs.Find(1);
                    Console.WriteLine("Context tracking changes of {0} entity.", ctx.ChangeTracker.Entries().Count());
                    DisplayTrackedEntities(ctx.ChangeTracker);
                    Console.WriteLine("Find Post");
                    var post = ctx.Posts.Find(1);
                    Console.WriteLine("Context tracking changes of {0} entities.", ctx.ChangeTracker.Entries().Count());
                    Console.WriteLine("Editing Posting");
                    post.Content = "Edited Content";
                    DisplayTrackedEntities(ctx.ChangeTracker);
                    Blog blog  = new Blog() { Name = "new Blog", Url = "#" };
                    Console.WriteLine("Adding New Post");
                    ctx.Blogs.Add(blog);
                    Console.WriteLine("Context tracking changes of {0} entities.", ctx.ChangeTracker.Entries().Count());
                    DisplayTrackedEntities(ctx.ChangeTracker);
                    Console.WriteLine("Remove Blog");
                    ctx.Blogs.Remove(blog1);
                    DisplayTrackedEntities(ctx.ChangeTracker);
                    Console.ReadKey();
                }
            }
    
            private static void DisplayTrackedEntities(DbChangeTracker changeTracker)
            {
                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("---------------------------------------");
            }
        }

    Best regards,

    Zhanlgong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, March 1, 2018 5:26 PM
    Wednesday, February 28, 2018 1:24 AM
    Moderator