Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询相同数据上下文时的不同结果_C#_Entity Framework - Fatal编程技术网

C# 查询相同数据上下文时的不同结果

C# 查询相同数据上下文时的不同结果,c#,entity-framework,C#,Entity Framework,为什么代码末尾的两个测试返回不同的结果?它们都查询相同的数据上下文,但其中一个返回“111”,另一个返回“222” //Make two contexts myAppContext context1 = new myAppContext(); myAppContext context2 = new myAppContext(); // Select and Modify Campaign id 1 using context 1 var SingleCam = context1.Campaig

为什么代码末尾的两个测试返回不同的结果?它们都查询相同的数据上下文,但其中一个返回“111”,另一个返回“222”

//Make two contexts
myAppContext context1 = new myAppContext();
myAppContext context2 = new myAppContext();

// Select and Modify Campaign id 1 using context 1
var SingleCam = context1.Campaigns.Where(c => c.CamId == 1).Single();
SingleCam.CamApplications = 111;
context1.Entry(SingleCam).State = System.Data.Entity.EntityState.Modified;
context1.SaveChanges();

// Select and Modify Campaign id 1 again using context 2
var SingleCam2 = context2.Campaigns.Where(c => c.CamId == 1).Single();
SingleCam2.CamApplications = 222;
context2.Entry(SingleCam2).State = System.Data.Entity.EntityState.Modified;
context2.SaveChanges();

// Access the Campaign through the same Context (1) and get two differnt results
var mycams = context1.Campaigns.ToList();
System.Diagnostics.Debug.WriteLine("Test 1 = " + mycams.Where(o => o.CamId == 1).Select(o => o.CamApplications).FirstOrDefault()); //returns 111
System.Diagnostics.Debug.WriteLine("Test 2 = " + context1.Campaigns.Where(o => o.CamId == 1).Select(o => o.CamApplications).FirstOrDefault()); //returns 222

你所看到的行为是完全正常的,即使它看起来很奇怪。虽然数据库中的值确实已更新为222,但对象中仍有一些过时的数据

当你打电话的时候

context1.SaveChanges();
更改被持久化到数据库(111)

然后你打电话

context2.SaveChanges();
这些更改被持久化到数据库(222)

基于现有对象(
context1.magnities
),该对象的值仍然为111。执行查询,但实体框架使用其缓存的实体值(感谢@GertArnold提出这一点)


将从数据库而不是缓存中检索数据,这就是您看到222的原因。

我的第一个问题是,为什么要手动修改实体的状态?实体框架已经跟踪到了这一点,手动修改可能会导致意外的副作用。我已经删除了修改状态的行,但仍然得到了相同的结果results@Evk第二个结果如何显示“222”,尽管它使用context1@programtreasures你是对的,我误读了密码。唯一的错误是“没有到数据库获取当前值的过程”。您将看到
var mycams=context1.campions.ToList();
执行查询,但EF不会覆盖缓存中实体的当前值。
var mycams
context1.Campaigns.Where(...)