-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Query: NRE when trying to project count of navigation typed as ICollection<T> #22701
Comments
Note: This should be handled in preprocessing visitor (probably in nav expansion) to remove redundant cast and convert Count to Queryable.Count(). |
One of my previous clients has just updated to EF Core 5 and this bug has hit them. They can get around it by using The client's code uses DDD-styled entity classes, with collections held in a backing fields and a I haven't built a complete example as you already have some simple code that fails, but here is a bare-bones example Entity class (bare-bones)This is my code. The client uses public class Book : EventsAndCreatedUpdated, ISoftDelete
{
//-----------------------------------------------
//relationships backing fields
private HashSet<Review> _reviews;
private HashSet<BookAuthor> _authorsLink;
private HashSet<Tag> _tags;
//scalar properties
public int BookId { get; private set; }
//... rest of code left out
//---------------------------------------
//relationships
public IReadOnlyCollection<Review> Reviews => _reviews?.ToList();
public IReadOnlyCollection<BookAuthor> AuthorsLink => _authorsLink?.ToList();
public IReadOnlyCollection<Tag> Tags => _tags?.ToList();
//... rest of code left out
} My testI wrote this to check that [Theory]
[InlineData(false)]
[InlineData(true)]
public void TestCountOfCollectionWithTypeChanged(bool countNoBrackets)
{
//SETUP
var options = SqliteInMemory.CreateOptions<BookDbContext>();
using (var context = new BookDbContext(options))
{
context.Database.EnsureCreated();
var bookId = context.SeedDatabaseFourBooks().Last().BookId;
context.ChangeTracker.Clear();
var query1 = context.Books
.Where(b => b.BookId == bookId);
var query2 = countNoBrackets
? query1.Select(b => b.Reviews.Count)
: query1.Select(b => b.Reviews.Count());
//ATTEMPT
int reviewCount;
try
{
reviewCount = query2.Single();
}
catch
{
countNoBrackets.ShouldBeTrue();
return;
}
//VERIFY
countNoBrackets.ShouldBeFalse();
_output.WriteLine(query2.ToQueryString());
reviewCount.ShouldEqual(2);
}
} The stacktrace is similar to the one shown (that's how my client found this issue)
My test, and my client tests, are using EF Core 5.0.1 |
Just hit the exact same issue.
My codebase is the same. Can confirm that the |
Could this be fixed in EF Core 6? This is a regression from EF Core 3.1 and keeps causing problems for people updating from the last LTS version. |
Could be fixed in #25577 |
query:
exception:
The text was updated successfully, but these errors were encountered: