其他分享
首页 > 其他分享> > dotnetcore EF 获取Entity状态

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

标签:Console,WriteLine,EF,ctx,Entity,dotnetcore,Student,new,Entry
来源: https://www.cnblogs.com/mryux/p/15860641.html