实体框架从linq select返回不正确的数据

时间:2015-07-30 08:45:43

标签: c# linq entity-framework

我是使用实体框架的新手,所以也许我做错了什么,但是下面的代码没有像我期望的那样工作:

PRODUCTEntities dbCustomerRefresh = new PRODUCTEntities();
Int16 delay = 5000;
while(true)
{
    var pendingFaults = (from f in dbCustomerRefresh.Faults where f.lRefreshStatus == 1 select f);
    foreach(Fault f in pendingFaults)
    {
        log.DebugFormat("Initial refresh status for fault {0} is {1}", f.lFaultID.ToString(), f.lRefreshStatus.ToString());
        f.lRefreshStatus = 2;
        log.DebugFormat("Changed Refresh status for fault: {0} to {1}", f.lFaultID.ToString(),f.lRefreshStatus.ToString());
    }
    log.DebugFormat("Saved {0} rows to LOSCH Database",dbCustomerRefresh.SaveChanges().ToString());
    Thread.Sleep(delay);
}

这是第一次循环循环时的预期效果,即。它会获取数据库中lRefreshStatus设置为1的每一行,将其更改为2并将其打印到调试。

然后我进入数据库并手动将lRefreshStatus更改为给定行的1。在下一轮循环中,我的程序会再次发现状态已经改变,但变量中的初始值是错误的。

2015-07-30 09:27:17,137 [10] DEBUG CustomerService - Initial refresh status for fault 101380 is 1
2015-07-30 09:27:17,137 [10] DEBUG CustomerService - Changed Refresh status for fault: 101380 to 2
2015-07-30 09:27:17,292 [10] DEBUG CustomerService - Saved 1 rows to PRODUCT Database
2015-07-30 09:27:22,297 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:27:27,304 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:27:32,313 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:27:37,318 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:27:42,327 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:27:47,336 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:27:52,341 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:27:57,346 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:02,356 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:07,361 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:12,366 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:17,371 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:22,378 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:27,384 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:32,390 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:37,396 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:42,405 [10] DEBUG CustomerService - Initial refresh status for fault 101380 is 2
2015-07-30 09:28:42,405 [10] DEBUG CustomerService - Changed Refresh status for fault: 101380 to 2
2015-07-30 09:28:42,405 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database
2015-07-30 09:28:47,410 [10] DEBUG CustomerService - Initial refresh status for fault 101380 is 2
2015-07-30 09:28:47,410 [10] DEBUG CustomerService - Changed Refresh status for fault: 101380 to 2
2015-07-30 09:28:47,411 [10] DEBUG CustomerService - Saved 0 rows to PRODUCT Database

奇怪的是。 select必须选择故障状态的值已更改为1或者它不在pendingFaults变量中,但是当我然后去访问该属性时,它设置为2.

它是不是从第一次开始处理旧的f变量,只是在第二次循环中再次给我回复?

1 个答案:

答案 0 :(得分:0)

像这样检查。

PRODUCTEntities dbCustomerRefresh = new PRODUCTEntities();
Int16 delay = 5000;
while(true)
{
    var pendingFaults = (from f in dbCustomerRefresh.Faults where f.lRefreshStatus == 1 select f);
    foreach(Fault f in pendingFaults)
    {
        log.DebugFormat("Initial refresh status for fault {0} is {1}", f.lFaultID.ToString(), f.lRefreshStatus.ToString());
        f.lRefreshStatus = 2;
        log.DebugFormat("Changed Refresh status for fault: {0} to {1}", f.lFaultID.ToString(),f.lRefreshStatus.ToString());
log.DebugFormat("Saved {0} rows to LOSCH Database",dbCustomerRefresh.SaveChanges().ToString());
    }

Thread.Sleep(delay);
}

看看它是否有所不同。