Efcore: InMemory: System.NullReferenceException on FindAsync

Created on 14 Jun 2019  路  9Comments  路  Source: dotnet/efcore

When I call the FindAsync method on an DbSet using a no-existing id, I'm getting an exception. I'm expecting null.

Exception message: System.NullReferenceException : Object reference not set to an instance of an object.
Stack trace: at lambda_method(Closure , QueryContext , IEnumerator`1 )
   at Microsoft.EntityFrameworkCore.InMemory.Query.Pipeline.InMemoryShapedQueryCompilingExpressionVisitor.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Pipeline.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at VinylStore.Catalog.Infrastructure.Repositories.ItemRepository.GetAsync(Guid id) in /Users/samuele.resca/Projects/EFCore.IssueSample/Catalog/src/VinylStore.Catalog.Infrastructure/Repositories/ItemRepository.cs:line 29
   at VinylStore.Catalog.Infrastructure.Tests.ItemRepositoryTests.should_returns_null_with_id_not_present() in /Users/samuele.resca/Projects/EFCore.IssueSample/Catalog/tests/VinylStore.Catalog.Infrastructure.Tests/ItemRepositoryTests.cs:line 56

Steps to reproduce

EFCore.IssueSample (please refer to the preview-6-null-ref-inmemory branch)

Please run:

dotnet build
dotnet test

Further technical details

EF Core version: 3.0.0-preview6.19304.10
Database Provider: Microsoft.EntityFrameworkCore.InMemory)
Operating system: macOS
IDE: VsCode

closed-fixed customer-reported type-bug

Most helpful comment

I've the same issue with:

<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0-preview7.19362.6" />

All 9 comments

@samueleresca First, many thanks for trying preview 6. If you haven't seen it already, there is some good information in the preview announcement post about the changes that are happening to LINQ queries for EF Core 3.0. Preview 6 is the first release containing these changes, which means we are both expecting things to be broken, but at the same time very grateful for everyone who tries the release and generated feedback.

As the blog posts says, LINQ queries to the in-memory database are currently not working for preview 6. Pinging @smitpatel for overall issue tracking this, or some more specific issue if appropriate.

Hi @ajcvickers, sorry, I've totally missed the announcement part about EF Core, my fault :). Should I close the issue?

@samueleresca I was attempting to thank you for filing the issue! I guess I need to change the my wording somewhat to make that clear.

I've the same issue with:

<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0-preview7.19362.6" />

Note: not fixed for cases where the entity is not found. (FirstOrDefault query goes down default path.)

Not fixed for me :

<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0-preview8.19405.11" />
 Aguacongas.AspNetCore.Authentication.EntityFramework.Test.DynamicManagerTest.RemoveAsync_should_remove_handler
  No source available
   Duration: 917 ms

  Message: 
    Assert.Throws() Failure
    Expected: typeof(Xunit.Sdk.NotNullException)
    Actual:   typeof(System.NullReferenceException): Object reference not set to an instance of an object.
    ---- System.NullReferenceException : Object reference not set to an instance of an object.
  Stack Trace: 
    lambda_method(Closure , ValueBuffer )
    SelectEnumerableIterator`2.MoveNext()
    AsyncEnumerator.MoveNextAsync()
    ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
    ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
    DynamicProviderStore`2.FindBySchemeAsync(String scheme, CancellationToken cancellationToken) line 177
    DynamicManagerTestBase`1.VerifyAddedAsync[TOptions](String schemeName, IServiceProvider provider) line 744
    ----- Inner Stack Trace -----
    lambda_method(Closure , ValueBuffer )
    SelectEnumerableIterator`2.MoveNext()
    AsyncEnumerator.MoveNextAsync()
    ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
    ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
    DynamicProviderStore`2.FindBySchemeAsync(String scheme, CancellationToken cancellationToken) line 177
    DynamicManagerTestBase`1.VerifyAddedAsync[TOptions](String schemeName, IServiceProvider provider) line 744

The code is :

        /// <summary>
        /// Finds scheme definition by scheme asynchronous.
        /// </summary>
        /// <param name="scheme">The scheme.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// An instance of TSchemeDefinition or null.
        /// </returns>
        /// <exception cref="System.ArgumentException">Parameter {nameof(scheme)}</exception>
        public virtual async Task<TSchemeDefinition> FindBySchemeAsync(string scheme, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (string.IsNullOrWhiteSpace(scheme))
            {
                throw new ArgumentException($"Parameter {nameof(scheme)} cannor be null or empty");
            }

            cancellationToken.ThrowIfCancellationRequested();
            var definition = await _context.FindAsync<TSchemeDefinition>(new[] { scheme }, cancellationToken);

            if (definition != null)
            {
                Deserialize(definition);
            }

            return definition;
        }

DynamicProviderStore on github

You can run the test : Aguacongas.AspNetCore.Authentication.EntityFramework.Test.DynamicManagerTest.RemoveAsync_should_remove_handler using the develop branch

@ajcvickers You should reopen

@aguacongas This fix was not included in preview 8. Please try with the daily builds

@ajcvickers thx, it's fixed in 3.0.0-preview9.* using daily build

Was this page helpful?
0 / 5 - 0 ratings