following query showing incorrect syntax near the keyword 'with'. if this statement is a common table expression. when using contains in linq after update .net 6.0 to 8.0
C#
var query = from entity in dbContext.YourEntity
where entity.SomeProperty.Contains(someValue)
select entity;

Jithu ThomasRank #437 · 5.7K reputationPosted Apr 4, 2024, 2:45 AM EDT
You're absolutely right. There's a known breaking change in Entity Framework Core (EF Core) 8.0 that affects LINQ queries using
Containswith older SQL Server versions. Here's the breakdown:The Issue:
Containsis translated to SQL for older SQL Servers (pre-2016) has changed.Affected Scenarios:
Containsto filter entities.The Code Snippet:
var query = from entity in dbContext.YourEntity where entity.SomeProperty.Contains(someValue) select entity;Solutions:
ALTER DATABASE [YourDatabaseName] SET COMPATIBILITY_LEVEL = 130; -- Set to 130 for SQL Server 2016Alternative Approaches (if compatibility changes are not preferred):
dbContext.Set().UseCompatibilityLevel(130); // Set to 130 for SQL Server 2016Containslogic using a different SQL expression supported by your older SQL Server version (e.g.,LIKE).Choosing the Right Solution:
The best approach depends on your specific circumstances. Upgrading SQL Server is generally recommended for long-term performance and functionality benefits. If upgrading isn't feasible, consider setting the compatibility level cautiously, with an understanding of potential trade-offs.
Remember to test your application thoroughly after making any changes to ensure everything works as expected.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 3, 2024, 6:35 PM EDT
I do not see with anywhere in the code you show.