EntityFrameworkCore.Cacheable
EntityFrameworkCore.Cacheable copied to clipboard
SaveChanges on entity not persisted in Cache
I've tried out Cacheable, and found that it doesn't seem to register new changes to entities. I kinds of expected Cacheable to purge any entity that was modified using the DbContext, so as to make sure it's fetched again.
Am I using this wrong?
I've noticed none of the tests test a SaveChanges() after the initial setup.
Steps to reproduce
Blog aa;
using (MyContext db = services.GetRequiredService<MyContext>())
{
// Cache the first entity in the DB
Blog c = db.Blogs.Cacheable(TimeSpan.FromMinutes(60)).First();
aa = c;
Console.WriteLine(c.Id);
Console.WriteLine(c.Name);
}
Blog bb;
using (MyContext db = services.GetRequiredService<MyContext>())
{
// Pick the first entity in the DB, without caching, so we can save changes
// Else #10 will kick in
Blog c = db.Blogs.First();
bb = c;
c.Name = DateTime.UtcNow.ToString();
Console.WriteLine(c.Name);
db.SaveChanges();
}
Blog cc;
using (MyContext db = services.GetRequiredService<MyContext>())
{
// Fetch the Blog again elsewhere, observe that Name has not changed
Blog c = db.Blogs.Cacheable(TimeSpan.FromMinutes(60)).First();
cc = c;
Console.WriteLine(c.Name);
}
// Here we see that the two Cached entries (aa, cc) are the same object
Console.WriteLine(ReferenceEquals(aa, bb));
Console.WriteLine(ReferenceEquals(aa, cc));
Console.WriteLine(ReferenceEquals(bb, cc));
Further technical details
EntityFrameworkCore.Cacheable version: (found in project.csproj or packages.config)
EF Core version: 2.2.4
IDE: VS 2019 (16.1.2)