Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Vector search
Note
Vector support was introduced in EF Core 10.0, and is only supported with SQL Server 2025 and above.
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).
To use the vector data type, simply add a .NET property of type SqlVector<float> to your entity type, specifying the dimensions as follows:
public class Blog
{
// ...
[Column(TypeName = "vector(1536)")]
public SqlVector<float> Embedding { get; set; }
}
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 contains IEmbeddingGenerator, which is an abstraction over embedding generators that has implementations for the major providers.
Once you've chosen your embedding generator and set it up, use it to generate embeddings and insert them as follows
IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator = /* Set up your preferred embedding generator */;
var embedding = await embeddingGenerator.GenerateVectorAsync("Some text to be vectorized");
context.Blogs.Add(new Blog
{
Name = "Some blog",
Embedding = new SqlVector<float>(embedding)
});
await context.SaveChangesAsync();
Finally, use the EF.Functions.VectorDistance() function to perform similarity search for a given user query:
var sqlVector = new SqlVector<float>(await embeddingGenerator.GenerateVectorAsync("Some user query to be vectorized"));
var topSimilarBlogs = context.Blogs
.OrderBy(b => EF.Functions.VectorDistance("cosine", b.Embedding, sqlVector))
.Take(3)
.ToListAsync();
Note
The built-in support in EF 10 replaces the previous 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.
The VECTOR_SEARCH() function (in preview) for approximate search with DiskANN is currently unsupported.