question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible SingleOrDefaultAsync issue?

See original GitHub issue

Hi Guys,

I bumped into a really strange issue when working on a project while using URF.core. I noticed that in my unit test a really simpel assert failed. But because there was a lot of custom code involved i first thought it was a mistake on my side so i skipped over it. Today I ran into the same issue again so I updated my fork of urf.core and added a simpel unit test:

[Fact]
        public async Task Product_Should_Not_Be_Null_After_A_Contains_Query()
        {
            // Arrange
            var repository = new Repository<Product>(_fixture.Context);

            // Act
            var products = await repository.Query().Where(p => p.ProductId == 1).SelectAsync();
            
            // Assert
            Assert.NotNull(products);

            // Act
            var product = await repository.Query().SingleOrDefaultAsync(s => s.ProductId == 2, new System.Threading.CancellationToken());

            // Assert
            Assert.NotNull(product);
        }

To my suprise this unit test is failing as well! Am i making a mistake or doing something wierd?

You can clone my fork to see it failing: https://github.com/Domitnator/URF.Core

I believe there is something going wrong in the SingleOrDefaultAsync because if i would replace that line with FindAsync:

[Fact]
        public async Task Product_Should_Not_Be_Null_After_A_Contains_Query()
        {
            // Arrange
            var repository = new Repository<Product>(_fixture.Context);

            // Act
            var products = await repository.Query().Where(p => p.ProductId == 1).SelectAsync();
            
            // Assert
            Assert.NotNull(products);

            // Act
            var product = await repository.FindAsync(2);

            // Assert
            Assert.NotNull(product);
        }

It works!

But it also works when i also request product with id 2 in the first query:

[Fact]
        public async Task Product_Should_Not_Be_Null_After_A_Contains_Query()
        {
            // Arrange
            var repository = new Repository<Product>(_fixture.Context);

            // Act
            var products = await repository.Query().Where(p => new List<int> { 1, 2 }.Contains(p.ProductId)).SelectAsync();
            
            // Assert
            Assert.NotNull(products);

            // Act
            var product = await repository.Query().SingleOrDefaultAsync(s => s.ProductId == 2, new System.Threading.CancellationToken());

            // Assert
            Assert.NotNull(product);
        }

So i’m not quite sure if it has anything to do with the SingleOrDefaultAsync method.

Hopefully you can help me out.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lelong37commented, May 30, 2019

@Domitnator if you could be so kind to issue a PR on this, that would be great.

1reaction
tonysneedcommented, May 10, 2019

@Domitnator, yes I think a PR would be good.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core SingleOrDefaultAsync compile time return type error
You should try the following to force the compiler to convert the return Type values and the possible default (null) type to Administrator...
Read more >
SingleOrDefaultAsync throws NullReference Exception if ...
SingleOrDefaultAsync throws a NullReference Exception, if no entry was found. Works if entry was found. "Object reference not set to an instance ...
Read more >
QueryableExtensions.SingleOrDefaultAsync Method
Asynchronously returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if...
Read more >
[Solved] When call get all repository pattern I dont found ...
The error message is perfectly clear - there is no SingleOrDefaultAsync extension method for the IEnumerable<T> interface.
Read more >
EntityFrameworkQueryableExten...
Asynchronously returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found