C# · 12月 27, 2021

c# – 实体框架5 – 为什么在PropertyValue设置为原始后,实体状态“修改”

我使用EF5,并且不知道为什么一个实体在将该实体的唯一更改的PropertyValue设置回原始值之后具有“修改”状态. using (TestDbContext context = new TestDbContext()) { string name = context.Person.First().Name; // count is 0 int count = context.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified); // Change Value context.Person.First().Name = “Test”; // count is 1 count = context.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified); // Revert Value context.Person.First().Name = name; context.ChangeTracker.DetectChanges(); // count is 1 count = context.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified); }

为什么?