• 4.跟踪


    默认情况下,ef在datacontext生命周期中跟踪已加载的实体

    当操作数据库现有数据时,才会跟踪

    如果在datacontext回收之前没savechanges,那么跟踪的状态就会丢失.

    实体得要有主键属性才能跟踪

    可以用下面的方法来跟踪datacontext的状态(Added Modified Deleted Unchanged Detached)

    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);
        }
    }

    输出Added

    using (var context = new BookStore())
    {
        Console.WriteLine("Adding Author");
        Author author = new Author() { Name = "Mark" };
         
        context.Authors.Add(author);
        Console.WriteLine("Context tracking changes of {0} entities.", context.ChangeTracker.Entries().Count());
        DisplayTrackedEntities(context.ChangeTracker);
    }

    输出Modified 

    using (var context = new BookStore())
    {
        Console.WriteLine("Update Author");
        Author author = context.Authors
            .FirstOrDefault();
         
        author.Name = "Russell";
        
        Console.WriteLine("Context tracking changes of {0} entities.", context.ChangeTracker.Entries().Count());
        DisplayTrackedEntities(context.ChangeTracker);
    
    }

    输出Deleted

    using (var context = new BookStore())
    {
        Console.WriteLine("Delete Author");
        Author author = context.Authors
            .FirstOrDefault();
         
        context.Authors.Remove(author);
        
        Console.WriteLine("Context tracking changes of {0} entities.", context.ChangeTracker.Entries().Count());
        DisplayTrackedEntities(context.ChangeTracker);
    
    }

    输出Unchanged 

    using (var context = new BookStore())
    {
        Author author = context.Authors
            .FirstOrDefault();
        
        Console.WriteLine("Context tracking changes of {0} entities.", context.ChangeTracker.Entries().Count());
        DisplayTrackedEntities(context.ChangeTracker);
    
    }

    输出Detached 

    Author author;
            
    using(var context = new BookStore())
    {
        author = context.Authors
            .FirstOrDefault();
    }
    
    using (var context = new BookStore())
    {                    
        Console.Write(context.Entry(author).State);
    }
  • 相关阅读:
    Python的包管理工具Pip
    [Reactive Programming] RxJS dynamic behavior
    [Reactive Programming] Using an event stream of double clicks -- buffer()
    [RxJS + AngularJS] Sync Requests with RxJS and Angular
    [Javascript] An Introduction to JSPM (JavaScript Package Manager)
    [Angular 2] 9. Replace ng-modle with #ref & events
    [React] Intro to inline styles in React components
    [AngualrJS + Webpack] Production Source Maps
    [AngularJS + Webpack] Uglifying your JavaScript
    [AngularJS + Webpack] Production Setup
  • 原文地址:https://www.cnblogs.com/nocanstillbb/p/11494560.html
Copyright © 2020-2023  润新知