You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: entity-framework/core/providers/sql-server/columns.md
+1-24Lines changed: 1 addition & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,11 +11,7 @@ This page details column configuration options that are specific to the SQL Serv
11
11
12
12
## Unicode and UTF-8
13
13
14
-
SQL Server 2019 introduced [introduced UTF-8](/sql/relational-databases/collations/collation-and-unicode-support#utf8) support, which allows storing UTF-8 data in `char` and `varchar` columns by configuring them with special UTF-8 collations.
15
-
16
-
### [EF Core 7.0](#tab/ef-core-7)
17
-
18
-
EF Core 7.0 includes first-class support for UTF-8 columns. To configure them, simply configure the column's type to `char` or `varchar`, specify a UTF-8 collation (ending with `_UTF8`), and specify that the column should be Unicode:
14
+
SQL Server 2019 introduced [introduced UTF-8](/sql/relational-databases/collations/collation-and-unicode-support#utf8) support, which allows storing UTF-8 data in `char` and `varchar` columns by configuring them with special UTF-8 collations. You can use UTF-8 columns with EF simply by configuring the column's type to `char` or `varchar`, specify a UTF-8 collation (ending with `_UTF8`), and specifying that the column should be Unicode:
In EF Core versions prior to 7.0, UTF-8 columns do not work out-of-the-box with EF Core's SQL Server provider. To map a string property to a `varchar(x)` column, the Fluent or Data Annotation API is typically used to disable Unicode ([see these docs](xref:core/modeling/entity-properties#unicode)). While this causes the correct column type to be created in the database, it also makes EF Core send database parameters in a way which is incompatible with UTF-8 data: `DbType.AnsiString` is used (signifying non-Unicode data), but `DbType.String` is needed to properly send Unicode data.
34
-
35
-
As a result, you'll have to configure the EF property as a regular `nvarchar` column, ensuring that parameters are sent with the correct `DbType`:
Once you've created the migration for the column, edit it and manually change the column's type from `nvarchar` to `varchar`.
47
-
48
-
***
49
-
50
27
## Sparse columns
51
28
52
29
Sparse columns are ordinary columns that have an optimized storage for null values, reducing the space requirements for null values at the cost of more overhead to retrieve non-null values.
# Vector search in the SQL Server EF Core Provider
9
+
10
+
## Vector search
11
+
12
+
> [!NOTE]
13
+
> Vector support was introduced in EF Core 10.0, and is only supported with SQL Server 2025 and above.
14
+
15
+
The SQL Server vector data type allows storing *embeddings*, which are representation of meaning that can be efficiently searched over for similarity, powering AI workloads such as semantic search and retrieval-augmented generation (RAG).
16
+
17
+
To use the `vector` data type, simply add a .NET property of type `SqlVector<float>` to your entity type, specifying the dimensions as follows:
Once your property is added and the corresponding column created in the database, you can start inserting embeddings. Embedding generation is done outside of the database, usually via a service, and the details for doing this are out of scope for this documentation. However, [the .NET Microsoft.Extensions.AI libraries](/dotnet/ai/microsoft-extensions-ai) contains [`IEmbeddingGenerator`](/dotnet/ai/microsoft-extensions-ai#create-embeddings), which is an abstraction over embedding generators that has implementations for the major providers.
52
+
53
+
Once you've chosen your embedding generator and set it up, use it to generate embeddings and insert them as follows
54
+
55
+
```c#
56
+
IEmbeddingGenerator<string, Embedding<float>>embeddingGenerator=/* Set up your preferred embedding generator */;
57
+
58
+
varembedding=awaitembeddingGenerator.GenerateVectorAsync("Some text to be vectorized");
59
+
context.Blogs.Add(newBlog
60
+
{
61
+
Name="Some blog",
62
+
Embedding=newSqlVector<float>(embedding)
63
+
});
64
+
awaitcontext.SaveChangesAsync();
65
+
```
66
+
67
+
Finally, use the [`EF.Functions.VectorDistance()`](/sql/t-sql/functions/vector-distance-transact-sql) function to perform similarity search for a given user query:
68
+
69
+
```c#
70
+
varsqlVector=newSqlVector<float>(awaitembeddingGenerator.GenerateVectorAsync("Some user query to be vectorized"));
> The built-in support in EF 10 replaces the previous [EFCore.SqlServer.VectorSearch](https://github.com/efcore/EFCore.SqlServer.VectorSearch) extension, which allowed performing vector search before the `vector` data type was introduced. As part of upgrading to EF 10, remove the extension from your projects.
79
+
>
80
+
> The [`VECTOR_SEARCH()`](/sql/t-sql/functions/vector-search-transact-sql) function (in preview) for approximate search with DiskANN is currently unsupported.
0 commit comments