Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InMemory: System.NullReferenceException on FindAsync #16099

Closed
samueleresca opened this issue Jun 14, 2019 · 9 comments · Fixed by #16860
Closed

InMemory: System.NullReferenceException on FindAsync #16099

samueleresca opened this issue Jun 14, 2019 · 9 comments · Fixed by #16860
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@samueleresca
Copy link

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

@ajcvickers
Copy link
Member

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

@samueleresca
Copy link
Author

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

@ajcvickers
Copy link
Member

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

@ajcvickers ajcvickers added this to the 3.0.0 milestone Jun 17, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview7 Jul 2, 2019
@aguacongas
Copy link

I've the same issue with:

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

@ajcvickers ajcvickers added the verify-fixed This issue is likely fixed in new query pipeline. label Jul 24, 2019
@ajcvickers ajcvickers self-assigned this Jul 24, 2019
@ajcvickers ajcvickers removed the verify-fixed This issue is likely fixed in new query pipeline. label Jul 29, 2019
@ajcvickers
Copy link
Member

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

ajcvickers added a commit that referenced this issue Jul 30, 2019
ajcvickers added a commit that referenced this issue Jul 30, 2019
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 30, 2019
ajcvickers added a commit that referenced this issue Jul 30, 2019
@smitpatel smitpatel removed their assignment Aug 6, 2019
@aguacongas
Copy link

aguacongas commented Aug 17, 2019

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

@aguacongas
Copy link

@ajcvickers You should reopen

@ajcvickers
Copy link
Member

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

@aguacongas
Copy link

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

@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview9 Aug 21, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview9, 3.0.0 Nov 11, 2019
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants