-
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
.Contains() on a simple List<T> within a LINQ query Where clause results in Microsoft.Data.SqlClient.SqlException: 'Incorrect syntax near '$'.' #31323
Comments
This doesn't repro for me with preview6. Can you please tweak the code below to make it fail, or provide a similar minimal, runnable code sample? await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
var ids = new List<Guid> { Guid.Parse("BE2A84A6-80EB-4846-B465-C226E28A676E") };
_ = context.MyTable
.Where(m => ids.Contains(m.Id))
.ToList();
public class BlogContext : DbContext
{
public DbSet<MyTable> MyTable { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class MyTable
{
public Guid Id { get; set; }
public string? Name { get; set; }
} |
One more thing - which version of SQL Server are you running against? |
I’m running against Azure SQL. Setting up an Azure SQL DB with this minimal table that we can both connect to... |
@MikeYeager Just:
|
Microsoft SQL Azure (RTM) - 12.0.2000.8 Jun 1 2023 13:36:49 Copyright (C) 2022 Microsoft Corporation |
I think we found the culprit. The exception happened against our database, but not the new one I created to test with. Our database has been around a long time and was originally on-premises. Even though it's now running in Azure, its Compatibility Level was set to SQL Server 2008 (100). Increasing the Compatibility Level to SQL Server 2014 (120) or higher fixes the problem. This has the same effect as I wonder if it would make sense to build in a Compatibility level check and a targeted exception message? |
Yeah: SELECT compatibility_level FROM sys.databases WHERE name = 'WideWorldImporters'; |
I think it must be 130 though |
OK, that all makes sense now.
Unfortunately, by design EF doesn't know the actual version/compat level of your database at the point where you execute a query (if it did, we wouldn't require you to manually specify the compat level with |
Hi all, I will raise an issue related to the following query. We have this "fancy" query, which normally translates to SQL in EF 7:
After updating to EF 8, we received the following exception:
I hope you will find out what is going on! |
@vadimkhm that looks like #32217. Can you please try the latest daily build and report whether the query works? |
Include your code
The following code run against any table and any Guid column will cause the exception. We use Guids for Ids in our database, but this exception seems to occur with int PKs also.
Include verbose output
I used the following logging options. Let me know if you want something different.
Include stack traces
Include provider and version information
EF Core version: 8.0.0-preview.6.23329.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 7.0.9
Operating system: Windows 11 Pro 10.022621 Build 22621
IDE: Visual Studio 2022 17.6.5
The text was updated successfully, but these errors were encountered: