EF Core custom query compiler and Future/FutureValue extensions
When implementing custom query compiler for ef core and then using Future() extension method resulting query returns NullReferenceException: Object reference not set to an instance of an object. It looks lke Z.EntityFramework.Plus.InternalExtensions.IsInMemoryQueryContext method can't get replaced query compiler.
IsInMemoryQueryContext source code
Stack trace:
NullReferenceException: Object reference not set to an instance of an object.
Z.EntityFramework.Plus.InternalExtensions.IsInMemoryQueryContext
Startup.cs:
services.AddDbContextPool<Context>(options =>
{
options.UseSqlServer(_connectionString);
options.ReplaceService<IQueryCompiler, CustomQueryCompiler>();
});
Query: var test = ctx.Posts.Future();
EF core version 2.1
Hello @vsopko ,
Do you think you could provide a project sample with this issue?
It will help my developer investigate the issue more efficiently.
We now always ask for a project sample even if the issue is easy to reproduce. As a free product, we must find some way to save time to offer an overall better experience for everyone and release fixes faster.
You can send it to: [email protected] if you need to keep the source private
Best Regards,
Jonathan
Performance Libraries
context.BulkInsert(list, options => options.BatchSize = 1000);
Entity Framework Extensions • Entity Framework Classic • Bulk Operations • Dapper Plus • LinqToSql Plus
Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval Function • SQL Eval Function
@JonathanMagnan Sorry, i can't find any time to extract and simplify code for this issue from our repository. To reproduce it you just need to:
- Add empty class CustomQueryCompiler.cs
using Microsoft.EntityFrameworkCore.Query.Internal;
public class CustomQueryCompiler: QueryCompiler
{}
- Add ReplaceService<IQueryCompiler, CustomQueryCompiler>(); to db context initialization in Startup.cs
@JonathanMagnan Some research gives us that while implementing custom compiler it's enougth to define same private fileds as in the base, from Microsofts After that your code in IsInMemoryQueryContext.cs can access it via reflection and everything is okay.
public class CustomQueryCompiler : QueryCompiler
{
private readonly IQueryContextFactory _queryContextFactory;
public CustomQueryCompiler(IQueryContextFactory queryContextFactory,
ICompiledQueryCache compiledQueryCache,
ICompiledQueryCacheKeyGenerator compiledQueryCacheKeyGenerator,
IDatabase database,
IDiagnosticsLogger<DbLoggerCategory.Query> logger,
ICurrentDbContext currentContext,
IQueryModelGenerator queryModelGenerator) : base(queryContextFactory, compiledQueryCache, compiledQueryCacheKeyGenerator, database, logger, currentContext, queryModelGenerator)
{
_queryContextFactory = queryContextFactory;
}
}