📜  Entity Framework-跟踪更改

📅  最后修改于: 2020-11-21 07:30:32             🧑  作者: Mango


实体框架提供了跟踪对实体及其关系所做的更改的功能,因此,当调用上下文的SaveChanges方法时,会在数据库上进行正确的更新。这是实体框架的关键功能。

  • 更改跟踪在将新记录添加到实体集合,修改或删除现有实体的同时跟踪更改。

  • 然后,所有更改都由DbContext级别保留。

  • 如果在DbContext对象被销毁之前未保存这些音轨更改,则会丢失它们。

  • DbChangeTracker类为您提供了有关上下文正在跟踪的当前实体的所有信息。

  • 要通过上下文跟踪任何实体,它必须具有主键属性。

在实体框架中,默认情况下启用更改跟踪。您还可以通过将DbContext的AutoDetectChangesEnabled属性设置为false来禁用更改跟踪。如果将此属性设置为true,则实体框架将维护实体的状态。

using (var context = new UniContextEntities()) {
   context.Configuration.AutoDetectChangesEnabled = true;
}

让我们看下面的示例,其中从数据库中检索学生及其注册。

class Program {

   static void Main(string[] args) {

      using (var context = new UniContextEntities()) {

         context.Configuration.AutoDetectChangesEnabled = true;
         Console.WriteLine("Retrieve Student");

         var student = (from s in context.Students where s.FirstMidName == 
            "Ali" select s).FirstOrDefault();

         string name = student.FirstMidName + " " + student.LastName;
         Console.WriteLine("ID: {0}, Name: {1}", student.ID, name);
         Console.WriteLine();
         Console.WriteLine("Retrieve all related enrollments");

         foreach (var enrollment in student.Enrollments) {
            Console.WriteLine("Enrollment ID: {0}, Course ID: {1}", 
               enrollment.EnrollmentID, enrollment.CourseID);
         }

         Console.WriteLine();

         Console.WriteLine("Context tracking changes of {0} entity.", 
            context.ChangeTracker.Entries().Count());

         var entries = context.ChangeTracker.Entries();

         foreach (var entry in entries) {
            Console.WriteLine("Entity Name: {0}", entry.Entity.GetType().Name);
            Console.WriteLine("Status: {0}", entry.State);
         }

         Console.ReadKey();
      }
   }
}

编译并执行上述示例后,您将收到以下输出。

Retrieve Student 
ID: 1, Name: Ali Alexander
Retrieve all related enrollments
       Enrollment ID: 1, Course ID: 1050
       Enrollment ID: 2, Course ID: 4022
       Enrollment ID: 3, Course ID: 4041
Context tracking changes of 4 entity.
Entity Name: Student
Status: Unchanged
Entity Name: Enrollment
Status: Unchanged
Entity Name: Enrollment
Status: Unchanged
Entity Name: Enrollment
Status: Unchanged

您可以看到,仅从数据库中检索了所有数据,这就是为什么所有实体的状态都不变的原因。

现在,让我们看另一个简单的示例,在该示例中,我们将再添加一个注册,并从数据库中删除一名学生。以下是添加新招生并删除一名学生的代码。

class Program {

   static void Main(string[] args) {

      using (var context = new UniContextEntities()) {

         context.Configuration.AutoDetectChangesEnabled = true;

         Enrollment enr = new Enrollment() { 
            StudentID = 1, CourseID = 3141 
         };

         Console.WriteLine("Adding New Enrollment");
         context.Enrollments.Add(enr);
         Console.WriteLine("Delete Student");

         var student = (from s in context.Students where s.ID == 
            23 select s).SingleOrDefault();

         context.Students.Remove(student);
         Console.WriteLine("");

         Console.WriteLine("Context tracking changes of {0} entity.", 
            context.ChangeTracker.Entries().Count());
         var entries = context.ChangeTracker.Entries();

         foreach (var entry in entries) {
            Console.WriteLine("Entity Name: {0}", entry.Entity.GetType().Name);
            Console.WriteLine("Status: {0}", entry.State);
         }

         Console.ReadKey();
      }
   }
}

编译并执行以上示例后,您将收到以下输出。

Adding New Enrollment
Delete Student
Context tracking changes of 2 entity.
Entity Name: Enrollment
Status: Added
Entity Name: Student
Status: Deleted

现在,您可以看到已将注册实体的状态设置为已添加,并且删除了学生实体的状态,因为已添加了新的注册,并且已从数据库中删除了一名学生。

我们建议您逐步执行上述示例,以更好地理解。