dotnetcore EF 获取Entity状态

通过EntityEntry可以获取Entity状态

using Microsoft.EntityFrameworkCore.ChangeTracking;

  static async Task Main(string[] args)
  {
      using (MyDbContext ctx = new MyDbContext())
      {
          var students = ctx.Students.Skip(10).Take(3).ToArray();

          Student a1 = students[0];
          Student a2 = students[1];
          Student a3 = students[2];

          Student a4 = new Student() { Name = "new_student1" };
          Student a5 = new Student() { Name = "new_student2" };

          a1.Name = "update_student";
          ctx.Remove(a3);
          ctx.Students.Add(a4);

          EntityEntry e1 = ctx.Entry(a1);

          Console.WriteLine(e1.State);
          Console.WriteLine(e1.DebugView.LongView);

          Console.WriteLine(ctx.Entry(a2).State);
          Console.WriteLine(ctx.Entry(a3).State);
          Console.WriteLine(ctx.Entry(a4).State);
          Console.WriteLine(ctx.Entry(a5).State);
      }
  }

dotnetcore EF 获取Entity状态

上一篇:第62篇-解释器与编译器适配(一)


下一篇:Redis 源码简洁剖析 06 - quicklist 和 listpack