Skip to content

iamhitya/efcore10-vector-search

Repository files navigation

EF Core 10 Vector Similarity Search (on .NET 10)

A minimal, production-ready example demonstrating how to implement vector similarity search (cosine/L2) with Entity Framework Core 10 on .NET 10. This repository is designed to showcase practical knowledge of EF Core 10 features, model design for embeddings, and efficient querying patterns for similarity search.

Highlights

  • EF Core 10 on .NET 10
  • Vector embedding storage using array or JSON columns
  • Efficient similarity querying (cosine similarity, Euclidean/L2 distance)
  • Migrations-driven schema and seed data for quick demos
  • Clean architecture and testable services

Tech Stack

  • .NET 10
  • EF Core 10
  • SQL database (SQLite or SQL Server)

Prerequisites

  • .NET 10 SDK installed
  • A SQL database (SQLite default; SQL Server optional)

Getting Started

  1. Clone the repository
    • git clone https://github.com/iamhitya/efcore10-vector-search
  2. Restore and build
    • dotnet restore
    • dotnet build
  3. Apply migrations
    • dotnet ef database update
  4. Run the sample
    • dotnet run --project EFCore10VectorSimilaritySearch/efcore10-vector-similarity-search.csproj

Configuration

  • Connection string can be set via environment variable ConnectionStrings__Default or appsettings.json.
  • By default, the sample uses SQLite for simplicity. Switch to SQL Server by updating provider configuration and connection string.

Project Structure

  • EFCore10VectorSimilaritySearch/
    • efcore10-vector-similarity-search.csproj — project file targeting .NET 10
    • Models/ — entity definitions (e.g., Document, Embedding)
    • Data/DbContext, configurations, migrations
    • Services/ — similarity search service, embedding utilities
    • Program.cs — entry point and demo workflow

Note: Exact folders may vary; the sample is organized to be clear and modular.

Data Model (Concept)

  • Document
    • Id, Title, Content
    • Vector (float[] or JSON-backed)
  • Indexes may include vector length and optional metadata filters.

Similarity Queries (Concept)

  • Cosine similarity: cos(vecA, vecB) = dot(vecA, vecB) / (||vecA|| * ||vecB||)
  • L2 (Euclidean) distance: sqrt(sum((a - b)^2))
  • Queries compute similarity between an input vector and stored vectors, returning top-k results.

Implementation approaches:

  • Client-side computation: load candidate vectors with filtering, compute similarity in-memory, order, and take top-k.
  • Provider-assisted: if using SQL Server with JSON + computed columns/UDFs, compute partial similarity server-side.

Generating Embeddings

This sample generates embeddings locally using Microsoft.Extensions.AI.Ollama with an Ollama server.

Usage Example

  • Seed some Document entities with Vector embeddings
  • Query with an input embedding (e.g., from a sentence transformer)
  • Return the top-k most similar documents with scores

The sample app demonstrates the above end-to-end.

About

Sample app to demonstrate how to implement vector similarity search with Entity Framework Core 10

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages