Skip to content

Commit b52f5e2

Browse files
committed
Document EF.Functions.JsonContains() (#5269)
See dotnet/efcore#37566
1 parent 61ae7ef commit b52f5e2

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

entity-framework/core/providers/sql-server/functions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ stringValue.Trim() | LTRIM(
232232
stringValue.TrimEnd() | RTRIM(@stringValue)
233233
stringValue.TrimStart() | LTRIM(@stringValue)
234234

235+
## JSON functions
236+
237+
.NET | SQL | Added in
238+
----------------------------------------------------------------- | --------------------------------------------------------- | --------
239+
EF.Functions.JsonContains(json, searchValue, path?, searchMode?) | JSON_CONTAINS(@json, @searchValue, @path?, @searchMode?) | EF Core 11.0
240+
235241
## Miscellaneous functions
236242

237243
.NET | SQL | Added in

entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ For more information, see the [full documentation on full-text search](xref:core
325325

326326
<a name="sql-server-json-contains"></a>
327327

328-
### Translate Contains over primitive collections using JSON_CONTAINS
328+
### Contains operations using JSON_CONTAINS
329329

330330
SQL Server 2025 introduced the [`JSON_CONTAINS`](/sql/t-sql/functions/json-contains-transact-sql) function, which checks whether a value exists in a JSON document. Starting with EF Core 11, when targeting SQL Server 2025, LINQ `Contains` queries over primitive (or scalar) collections stored as JSON are translated to use this function, replacing the previous, less efficient `OPENJSON`-based translation.
331331

@@ -368,3 +368,27 @@ WHERE JSON_CONTAINS([b].[Tags], 'ef-core') = 1
368368

369369
> [!NOTE]
370370
> `JSON_CONTAINS` does not support searching for null values. As a result, this translation is only applied when EF can determine that at least one side is non-nullable — either the item being searched for (e.g. a non-null constant or a non-nullable column), or the collection's elements. When this cannot be determined, EF falls back to the previous `OPENJSON`-based translation.
371+
372+
#### EF.Functions.JsonContains()
373+
374+
In the section above, EF Core automatically translates LINQ `Contains` queries over primitive collections to use the SQL Server `JSON_CONTAINS` function. In some cases, however, you may want to directly invoke `JSON_CONTAINS` yourself, for example to search for a value at a specific path, or to specify a search mode. For these cases, EF Core 11 introduces `EF.Functions.JsonContains()`.
375+
376+
The following query checks whether a blog's JSON data contains a specific value at a given path:
377+
378+
```csharp
379+
var blogs = await context.Blogs
380+
.Where(b => EF.Functions.JsonContains(b.JsonData, 8, "$.Rating") == 1)
381+
.ToListAsync();
382+
```
383+
384+
This generates the following SQL:
385+
386+
```sql
387+
SELECT [b].[Id], [b].[Name], [b].[JsonData]
388+
FROM [Blogs] AS [b]
389+
WHERE JSON_CONTAINS([b].[JsonData], 8, N'$.Rating') = 1
390+
```
391+
392+
`EF.Functions.JsonContains()` accepts the JSON value to search in, the value to search for, and optionally a JSON path and a search mode. It can be used with scalar string properties, complex types, and owned entity types mapped to JSON columns.
393+
394+
For the full `JSON_CONTAINS` SQL Server documentation, see [`JSON_CONTAINS`](/sql/t-sql/functions/json-contains-transact-sql).

0 commit comments

Comments
 (0)