Vector similarity search is becoming increasingly important in modern applications, powering AI workloads such as semantic search and retrieval-augmented generation (RAG).
EF Core 10 introduces native vector similarity search support, enabling semantic search directly within your data layer using SQL Server 2025's vector capabilities.
This eliminates the need for a separate vector database and allows you to perform similarity searches alongside your traditional relational queries.
VECTOR Data Type Configuration
EF Core 10 supports SQL Server's native VECTOR data type for storing embeddings:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Document>(entity =>
{
entity.Property(e => e.Embedding)
.HasColumnType("VECTOR(1024)");
});
}
The VECTOR data type stores high-dimensional vectors efficiently and supports similarity calculations directly in SQL Server.
SqlVector<T> Type
The SqlVector<T> type provides strongly-typed vector operations:
public sealed class Document
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public SqlVector<float> Embedding { get; set; }
}
SqlVector<T> provides type safety and prevents accidental mixing of incompatible vector dimensions or data types.
Generating Embeddings
For generating the embeddings, I used local AI models powered by the Microsoft.Extensions.AI.Ollama package.
To use Ollama, we first need to download and install it.
Now we need to install the model which we plan to use for generating our embeddings. You can do this with the following command:
ollama pull mxbai-embed-large
I ended up going with the mxbai-embed-large model.
Now, we just need to register our embedding generator to make it available throughout the application.
builder.Services.AddEmbeddingGenerator(
new OllamaEmbeddingGenerator(builder.Configuration["Ollama:Url"], "mxbai-embed-large"));
"Ollama": {
"Url": "http://localhost:11434/"
}
With this, we are ready to generate our embeddings. The model creates solid 1024-dimensional vectors perfect for semantic search.
app.MapPost("/api/search", async (
string query,
ApplicationDbContext db,
IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator) =>
{
var embeddingResult = await embeddingGenerator.GenerateAsync([query]);
var queryVector = new SqlVector<float>(embeddingResult.Single().Vector);
});
You can generate them by injecting IEmbeddingGenerator<string, Embedding<float>> and using its GenerateAsync method.
Ollama handles all the work under the hood, so we don’t need any external services or additional infrastructure.
Vector Distance Functions
EF Core 10 introduces VectorDistance() for similarity calculations:
var results = await db.Documents
.OrderBy(d => EF.Functions.VectorDistance("cosine", d.Embedding, queryVector))
.Take(5)
.Select(d => new
{
d.Id,
d.Title,
d.Content,
Distance = EF.Functions.VectorDistance("cosine", d.Embedding, queryVector)
})
.ToListAsync();
EF Core translates this into efficient SQL using the VECTOR_DISTANCE() function.
Supported distance metrics include:
- "cosine" - Cosine similarity (measures angle between vectors)
- "euclidean" - Euclidean distance (straight-line distance)
- "manhattan" - Manhattan distance (sum of absolute differences)
Conclusion
With EF Core 10, vector similarity search becomes easier than ever.
SQL Server 2025’s native VECTOR data type, strongly typed SqlVector
If you want to check out examples I created, you can find the source code here:
Source CodeI hope you enjoyed it, subscribe and get a notification when new blog is up!
