简体   繁体   中英

Entity framework incorrect data returned from linq select

I'm new to using entity framework, so maybe I'm doing something really wrong, but the following code isn't working the way I'd expect it to:

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);
}

This works as expected the first time round the loop, Ie it picks up every row in the DB that has the lRefreshStatus set to 1, changes it to 2 and prints it to debug.

I then go into the database and manually change the lRefreshStatus back to 1 for a given row. On the next loop round, my program picks up that the status has changed again, but the initial values in the variable are wrong.

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

Its bizarre. The select must pick up that the value of the fault status has changed to 1 or it wouldn't be in the pendingFaults variable, but when I then go to access that property, its set to 2.

Does it not dispose of the old f variable from the first time around, and just give me that back again on the second loop?

Check like this.

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);
}

See if it makes difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM