-
Notifications
You must be signed in to change notification settings - Fork 10
Description
Overview
The DbFunctionsExtensions.Count(this DbFunctions _, OverClause over) method's declared return type is int but in PostgreSQL, the count ( * ) method returns a bigint as documented here.
Affected Versions
The bug was encountered in version 9.0.1-beta but is likely present in older versions as well.
EF Core version used: 9.0.6.
Npgsql version used: 9.0.4.
Postgres version used: 16.9.
.NET version: 8.0.
Expected Behavior
The Count() window function returns the total number of rows in the partition as an int.
Observed Behavior
The Count() window function throws an error while trying to map what is actually a SQL bigint to a C# int.
Steps to Reproduce
Project the Count window function onto a property of type int.
Example
// User.cs
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
public class UserWithCount
{
public int Count { get; set; }
public User User { get; set; }
}
// AppContext.cs
public class AppContext : DbContext
{
public DbSet<User> Users { get; set; }
public UserWithCount[] UsersWithName()
{
// Return every user along with the number of other users who share their name.
return Users.Select(u => new UserWithCount
{
Count = EF.Functions.Count(EF.Functions.Over().PartitionBy(u.Name)),
User = u
}).ToArray();
}
}Mitigation
Projecting the Count function onto a property of type long works as intended. It is unknown if values greater than the maximum integer value will be handled properly.