Repository navigation
Closed
Description
The following C# code:
var ids = new int [] { 11, 22, 33, 44, 55 };
await context.Orders.Where(x => ids.Contains(x.OrderID)).ToListAsync();Is producing the following SQL:
SELECT o."OrderID", o."CustomerID", o."EmployeeID", o."OrderDate"
FROM "Orders" AS o
WHERE COALESCE(o."OrderID" = ANY (@__ids_0), FALSE)This results in queries not being optimized/indexes not being used.
Code to reproduce:
- Create project.
dotnet new webapi ExampleIssueNpgsql
cd ExampleIssueNpgsql
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.Extensions.Logging.Console
rm Startup.cs- Place this into Program.cs and set proper database credentials:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
namespace ExampleIssueNpgsql
{
public class Program
{
public class OrderDetail
{
public int OrderDetailID { get; set; }
public int OrderID { get; set; }
public int ProductID { get; set; }
public int Quantity { get; set; }
public Order Order { get; set; }
}
public class Order
{
public int OrderID { get; set; }
public int CustomerID { get; set; }
public int EmployeeID { get; set; }
public DateTime OrderDate { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
}
public class MyContext : DbContext
{
public DbSet<OrderDetail> OrderDetails { get; set; }
public DbSet<Order> Orders { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseNpgsql(@"Host=localhost;Database=issue-db;Username=postgres;Password=password")
.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()));
}
}
public static async Task Main(string[] args)
{
using (var context = new MyContext()) {
var ids = new int [] { 11, 22, 33, 44, 55 };
await context.Orders.Where(x => ids.Contains(x.OrderID)).ToListAsync();
}
}
}
}- Run the following to create test schema:
dotnet ef migrations add InitialCreate
# NOTE: Might require fixing generated migration before running it
dotnet ef database update- Run the program
dotnet runOutput will be the following:
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 3.1.1 initialized 'MyContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (10ms) [Parameters=[@__ids_0='?' (DbType = Object)], CommandType='Text', CommandTimeout='30']
SELECT o."OrderID", o."CustomerID", o."EmployeeID", o."OrderDate"
FROM "Orders" AS o
WHERE COALESCE(o."OrderID" = ANY (@__ids_0), FALSE)
COALESCE shouldn't have been generated.
Reacted by Peter Grman
Metadata
Metadata
Assignees
Labels
No labels
Activity
roji commentedon Feb 6, 2020
Duplicate of #1152