ExecuteUpdate is not working with Sqlite
ExecuteUpdate is not working with Sqlite in unit tests. ExecuteDelete works, but ExecuteUpdate simply fails to update the specified fields even though it reports affecting a number of records.
Here is the logged output from EF for the command:
dbug: 1/26/2023 14:38:16.240 CoreEventId.QueryCompilationStarting[10111] (Microsoft.EntityFrameworkCore.Query)
Compiling query expression:
'DbSet<Budget>()
.Where(b => b.BudgetId == 1)
.ExecuteUpdate(b => b.SetProperty
Perhaps the lack of an alias for SET "Name" is the issue?
Well, I think the generated SQL should work, but even though the log says the statement was executed, the row isn't actually updated. I'm using Microsoft.EntityFramework.Sqlite 7.0.2.
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
If you follow this getting started tutorial: https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli
But modify Program.cs to do the following:
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using var db = new BloggingContext();
// Create
Console.WriteLine("Inserting a new blog");
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();
db.Blogs.ExecuteUpdate(b => b.SetProperty(p => p.Url, "https://www.testupdated.com"));
//the update will not be reflected here.
var blogs = db.Blogs.ToList();
I realize now that it may just be the change tracker is not reflecting the ExecuteUpdate. When I call bpmsContext.ChangeTracker.Clear(), the updates are reflected. This isn't the same behavior as ExecuteDelete though. In that case, db.Blogs.ToList() will return the right records even without the call to ChangeTracker.Clear().
@drjackson Queries using a DbSet are always executed against the database, so if rows have been deleted in the database, then the query results will reflect that. But any entity that is already tracked will not be re-created or updated based on the query results.
I assume this is expected behavior then and so I'll close this issue. Thanks for your help.