我读到过一篇关于AsNoTrackingWithIdentityResolve()方法的文章,它是一种无需跟踪的身份解析方法,并且当使用它时,与上下文不同的更改跟踪器将在后台运行,以将引用同一行的实体解析为一个实例.
我试图通过运行以下代码来验证这一点,但实例并未解析为一个实例
// Initially the table is empty
Employee emp1 = new Employee()
{
FirstName = "name1",
LastName = "name2",
Salary = 11111
};
context.Add(emp1);
context.SaveChanges();
Employee emp2 = (from e in context.Employees
where e.FirstName == "name1"
select e).SingleOrDefault();
// Identity resolution takes place
Console.WriteLine(emp1 == emp2); // True
Employee emp3 = (from e in context.Employees
where e.FirstName == "name1"
select e).AsNoTrackingWithIdentityResolution().SingleOrDefault();
// Expected to give True due to using AsNoTrackingWtihIdentityResolution()
Console.WriteLine(emp1 == emp3); // but found False
Employee emp4 = (from e in context.Employees
where e.FirstName == "name1"
select e).AsNoTrackingWithIdentityResolution().SingleOrDefault();
// Also no identity resolution happened
Console.WriteLine(emp4 == emp3); // False
The reference comparison only gave true when the two compared instances (which are resolved to one instance) were tracked by the context change tracker, while the other instances weren't resolved to one instance. I'm using EF core 8.0.2个
所以现在:
- 为什么在使用带有查询的AsNoTrackingWithIdentitySolutions()方法时,引用同一行的实例没有解析为一个实例,因为这是其预期的功能之一?
- 为什么在使用这种方法时会有一个独立的跟踪器在后台运行,以至于使用这种方法的目标之一就是停止任何跟踪?