I'm having an issue with SQLite DB access. I get wrong data returned. This is a single app running as a "service" that is accessed by stations via TCP/IP sockets Client/Server. This app does all of the DB Access. The "server" is using .net 6.x and EF Core 6.x with SQLite.
What I see is that I'll get the previous data from the DB as opposed to the current state.
When I use a linq on the context as follows:
Account account = custDB.Accounts.AsNoTracking()
.Where(a => a.AcctNo == accountNum)
.FirstOrDefault();
I'll get whats on Disk fine but after a DB update I still get the same initial values.
I tried using .AsNoTracking() in my Linq Select and I got the current data in the DB every time. But when I Update the DB the new data is not written to DB and I get no errors thrown.
Here's the sequence:
- Lookup record using linq:
Account account = custDB.Accounts.AsNoTracking()
.Where(a => a.AcctNo == accountNum)
.FirstOrDefault();
- Perform tasks to update returned object account
- pass updated account object to update routine
private void UpdateAccountTable(Account account)
{
Account acct;
try
{
acct = custDB.Accounts.Where(a => a.ID == account.ID).Single();
}
catch (Exception ex)
{
throw new DbUpdateException(Message);
}
try
{
acct.AcctNo = account.AcctNo;
acct.Active = account.Active;
acct.Address1 = account.Address1;
acct.City = account.City;
acct.IsAPIAcct = account.IsAPIAcct;
acct.Name = account.Name;
acct.Phone = account.Phone;
acct.SerialNo = account.SerialNo;
acct.State = account.State;
acct.SWSIMID = account.SWSIMID;
acct.SWSIMPhrase = account.SWSIMPhrase;
acct.Zip = account.Zip;
acct.IsVerified = account.IsVerified;
acct.CleanHash = account.CleanHash;
acct.OverRideHash = account.OverRideHash;
acct.Zip4 = account.Zip4;
custDB.SaveChanges();
}
catch (Exception ex)
{
throw new DbUpdateException(Message);
}
}
There are no errors thrown. Results are it looks good programmatically, but the DB is never updated with the new values.
Now if I remove the .AsNoTracking() in the first select linq the DB is correctly updated.
What am I doing wrong.