efcore: Precompiled queries assume the model for a given DbContext will never change
Steps to reproduce
when I use EF.CompileQuery to generate a Query it fails to find the result. executing the linq normally returns the result.
I also verified that it is not a threading issue. the item is there all the time and if im setting the debugger to call FindItemQuery again it still returns null.
The behavior is observed in a unittest. using xunit. If the test is executed alone it works most of the time. If I execute all the tests it sometimes returns null
var services = new ServiceCollection()
.AddDbContext<DbContext>(o => o.UseInMemoryDatabase(nameof(TestClass)+ nameof(TestMethod)))
.BuildServiceProvider();
private static readonly Func<DbContext, string, Item> FindItemQuery =
EF.CompileQuery<DbContext, string, Item>((ctx, key) => ctx.Items
.FirstOrDefault(x => x.Key == key));
protected override Item FindItem(DbContext dbContext, string key)
{
var found = FindItemQuery(dbContext, key);
if (found == null)
{
var xxx = dbContext.Items.FirstOrDefault(t => t.Key== key);
//xxx is not null
}
return found ?? new Item()
{
Key = key
};
}
Further technical details
EF Core version: Microsoft.EntityFrameworkCore.InMemory Version=“2.1.2” Operating system: Win 10 IDE: (Visual Studio 2017 15.8)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 28 (15 by maintainers)
Commits related to this issue
- fixed compiled queries not returning a result when applied to different in memory database - locate inmemory tables by entitytype.toString() - removed option useNameMatching as the new method works f... — committed to JanEggers/EntityFramework by JanEggers 6 years ago
- fixed compiled queries not returning a result when applied to different in memory database - locate inmemory tables by entitytype.toString() - removed option useNameMatching as the new method works f... — committed to JanEggers/EntityFramework by JanEggers 6 years ago
- Throw if CompiledQuery is used with different models Fixes #13483 — committed to dotnet/efcore by ajcvickers 2 years ago
- Throw if CompiledQuery is used with different models Fixes #13483 — committed to dotnet/efcore by ajcvickers 2 years ago
This is working in current daily. Not sure what to add for regression test here.