Created
March 30, 2022 09:58
-
-
Save joakimriedel/35cfad1e71afe795c42c1a2b17ccab58 to your computer and use it in GitHub Desktop.
EF Core bug: Multiple counts in one select
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.Logging; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace EfCoreBug | |
{ | |
class Program | |
{ | |
public static readonly ILoggerFactory MyLoggerFactory | |
= LoggerFactory.Create(builder => | |
{ | |
builder.AddConsole(); | |
}); | |
static async Task Main(string[] args) | |
{ | |
using (var context = new ClientContext()) | |
{ | |
context.Database.EnsureDeleted(); | |
context.Database.EnsureCreated(); | |
// info: Microsoft.EntityFrameworkCore.Database.Command[20101] | |
// Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] | |
// SELECT COUNT(*) | |
// FROM [Users] AS [u] | |
// info: Microsoft.EntityFrameworkCore.Database.Command[20101] | |
// Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] | |
// SELECT COUNT(*) | |
// FROM [Animals] AS [a] | |
// info: Microsoft.EntityFrameworkCore.Database.Command[20101] | |
// Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] | |
// SELECT TOP(1) 1 | |
// FROM [Users] AS [u] | |
var bad = await context.Users.Select(u => new { | |
userCount = context.Users.Count(), | |
animalCount = context.Animals.Count() | |
}).FirstAsync(); | |
// info: Microsoft.EntityFrameworkCore.Database.Command[20101] | |
// Executed DbCommand (13ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] | |
// SELECT TOP(1) ( | |
// SELECT COUNT(*) | |
// FROM [Users] AS [u0]) AS [userCount], ( | |
// SELECT COUNT(*) | |
// FROM [Animals] AS [a]) AS [animalCount] | |
// FROM [Users] AS [u] | |
var good = await context.Users.Select(u => new { | |
userCount = context.Users.Where(x => true).Count(), | |
animalCount = context.Animals.Where(x => true).Count() | |
}).FirstAsync(); | |
} | |
} | |
public class User | |
{ | |
public int Id { get; set; } | |
} | |
public class Animal | |
{ | |
public int Id { get; set; } | |
} | |
class ClientContext : DbContext | |
{ | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
// TO USE SQL | |
optionsBuilder | |
.UseLoggerFactory(MyLoggerFactory) | |
.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=MultipleCounts;Trusted_Connection=True;Connect Timeout=30") | |
.EnableSensitiveDataLogging(true); | |
// TO USE INMEMORY | |
//optionsBuilder | |
// .UseLoggerFactory(MyLoggerFactory) | |
// .UseInMemoryDatabase(Guid.NewGuid().ToString()); | |
} | |
protected override void OnModelCreating(ModelBuilder builder) | |
{ | |
base.OnModelCreating(builder); | |
builder.Entity<User>().HasData(new User() | |
{ | |
Id = 1, | |
}); | |
builder.Entity<Animal>().HasData(new Animal() | |
{ | |
Id = 1, | |
}); | |
} | |
public DbSet<User> Users { get; set; } | |
public DbSet<Animal> Animals { get; set; } | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<LangVersion>latest</LangVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.3" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.3" /> | |
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment