EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

EF Core custom query compiler and Future/FutureValue extensions

Open vsopko opened this issue 8 years ago • 3 comments

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(IQueryable query) Z.EntityFramework.Plus.QueryFutureExtensions.Future(IQueryable query)

Startup.cs:

services.AddDbContextPool<Context>(options =>
{
    options.UseSqlServer(_connectionString);
    options.ReplaceService<IQueryCompiler, CustomQueryCompiler>();
});

Query: var test = ctx.Posts.Future();

EF core version 2.1

vsopko avatar Oct 16 '18 15:10 vsopko

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 avatar Oct 16 '18 15:10 JonathanMagnan

@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:

  1. Add empty class CustomQueryCompiler.cs
using Microsoft.EntityFrameworkCore.Query.Internal;
public class CustomQueryCompiler: QueryCompiler
{}
  1. Add ReplaceService<IQueryCompiler, CustomQueryCompiler>(); to db context initialization in Startup.cs

vsopko avatar Oct 16 '18 15:10 vsopko

@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;
        }
}

vsopko avatar Oct 26 '18 13:10 vsopko