Pgvector.EntityFrameworkCore 0.1.2

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Pgvector.EntityFrameworkCore --version 0.1.2
                    
NuGet\Install-Package Pgvector.EntityFrameworkCore -Version 0.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pgvector.EntityFrameworkCore" Version="0.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pgvector.EntityFrameworkCore" Version="0.1.2" />
                    
Directory.Packages.props
<PackageReference Include="Pgvector.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Pgvector.EntityFrameworkCore --version 0.1.2
                    
#r "nuget: Pgvector.EntityFrameworkCore, 0.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Pgvector.EntityFrameworkCore@0.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Pgvector.EntityFrameworkCore&version=0.1.2
                    
Install as a Cake Addin
#tool nuget:?package=Pgvector.EntityFrameworkCore&version=0.1.2
                    
Install as a Cake Tool

pgvector-dotnet

pgvector support for C#

Supports Npgsql, Dapper, and Entity Framework Core

Build Status

Getting Started

Follow the instructions for your database library:

Npgsql

Run:

dotnet add package Pgvector

Import the library

using Pgvector.Npgsql;

Create a connection

var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
dataSourceBuilder.UseVector();
await using var dataSource = dataSourceBuilder.Build();

var conn = dataSource.OpenConnection();

Enable the extension

await using (var cmd = new NpgsqlCommand("CREATE EXTENSION IF NOT EXISTS vector", conn))
{
    await cmd.ExecuteNonQueryAsync();
}

conn.ReloadTypes();

Create a table

await using (var cmd = new NpgsqlCommand("CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3))", conn))
{
    await cmd.ExecuteNonQueryAsync();
}

Insert a vector

await using (var cmd = new NpgsqlCommand("INSERT INTO items (embedding) VALUES ($1)", conn))
{
    var embedding = new Vector(new float[] { 1, 1, 1 });
    cmd.Parameters.AddWithValue(embedding);
    await cmd.ExecuteNonQueryAsync();
}

Get the nearest neighbors

await using (var cmd = new NpgsqlCommand("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", conn))
{
    var embedding = new Vector(new float[] { 1, 1, 1 });
    cmd.Parameters.AddWithValue(embedding);

    await using (var reader = await cmd.ExecuteReaderAsync())
    {
        while (await reader.ReadAsync())
        {
            Console.WriteLine((Vector)reader.GetValue(0));
        }
    }
}

Add an approximate index

await using (var cmd = new NpgsqlCommand("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)", conn))
{
    await cmd.ExecuteNonQueryAsync();
}

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

See a full example

Dapper

Run:

dotnet add package Pgvector.Dapper

Import the library

using Pgvector.Dapper;
using Pgvector.Npgsql;

Create a connection

SqlMapper.AddTypeHandler(new VectorTypeHandler());

var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
dataSourceBuilder.UseVector();
await using var dataSource = dataSourceBuilder.Build();

var conn = dataSource.OpenConnection();

Enable the extension

conn.Execute("CREATE EXTENSION IF NOT EXISTS vector");
conn.ReloadTypes();

Define a class

public class Item
{
    public int Id { get; set; }
    public Vector? Embedding { get; set; }
}

Create a table

conn.Execute("CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3))");

Insert a vector

var embedding = new Vector(new float[] { 1, 1, 1 });
conn.Execute(@"INSERT INTO items (embedding) VALUES (@embedding)", new { embedding });

Get the nearest neighbors

var embedding = new Vector(new float[] { 1, 1, 1 });
var items = conn.Query<Item>("SELECT * FROM items ORDER BY embedding <-> @embedding LIMIT 5", new { embedding });
foreach (Item item in items)
{
    Console.WriteLine(item.Embedding);
}

Add an approximate index

conn.Execute("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)");
// or
conn.Execute("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)");

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

See a full example

Entity Framework Core

Run:

dotnet add package Pgvector.EntityFrameworkCore

Import the library

using Pgvector.EntityFrameworkCore;

Enable the extension

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.HasPostgresExtension("vector");
}

Configure the connection

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseNpgsql("connString", o => o.UseVector());
}

Define a model

public class Item
{
    [Column(TypeName = "vector(3)")]
    public Vector? Embedding { get; set; }
}

Insert a vector

ctx.Items.Add(new Item { Embedding = new Vector(new float[] { 1, 1, 1 }) });
ctx.SaveChanges();

Get the nearest neighbors

var embedding = new Vector(new float[] { 1, 1, 1 });
var items = await ctx.Items.OrderBy(x => x.Embedding!.L2Distance(embedding)).Take(5).ToListAsync();
foreach (Item item in items)
{
    if (item.Embedding != null)
    {
        Console.WriteLine(item.Embedding);
    }
}

Also supports MaxInnerProduct and CosineDistance

Add an approximate index

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Item>()
        .HasIndex(i => i.Embedding)
        .HasMethod("ivfflat") // or hnsw
        .HasOperators("vector_l2_ops");
}

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

See a full example

History

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector-dotnet.git
cd pgvector-dotnet
createdb pgvector_dotnet_test
dotnet test
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (16)

Showing the top 5 NuGet packages that depend on Pgvector.EntityFrameworkCore:

Package Downloads
SoftwareExtravaganza.Whizbang.Data.EFCore.Postgres

PostgreSQL Entity Framework Core integration for Whizbang with source-generated configuration.

FluxIndex.Storage.PostgreSQL

PostgreSQL with pgvector storage provider for FluxIndex

Hx.Abp.Attachment.EntityFrameworkCore

Package Description

QimErp.Shared.Common

Shared common library for QimErp microservices ERP system. Contains common entities, contracts, services, and utilities used across all QimErp modules.

Stratara.EventSourcing.EntityFrameworkCore

EF Core persistence for the Stratara event-sourced stack on PostgreSQL — write-store, read-store, ASP.NET Identity store — including a shared identity store with its migrations — shared conventions, value generators, and UnitOfWork primitives. Targets Npgsql; maps pgvector types for a model that uses them, and needs no database extension otherwise.

GitHub repositories (8)

Showing the top 8 popular GitHub repositories that depend on Pgvector.EntityFrameworkCore:

Repository Stars
dotnet/eShop
A reference .NET application implementing an eCommerce site
bitfoundation/bitplatform
Build all of your apps using what you already know and love ❤️
CervantesSec/cervantes
Cervantes is an open-source, collaborative platform designed specifically for pentesters and red teams. It serves as a comprehensive management tool, streamlining the organization of projects, clients, vulnerabilities, and reports in a single, centralized location.
thangchung/practical-dotnet-aspire
The practical .NET Aspire builds on the coffeeshop app business domain
Azure-Samples/eShopOnAzure
A variant of https://github.com/dotnet/eShop that uses Azure services
daniel3303/Equibles
Self-hosted, open-source financial data MCP server for AI agents — SEC filings, XBRL financials, 13F holdings, insider & congressional trades, short interest, FRED, CFTC/CBOE and daily prices across 64 MCP tools. Equibles Cloud adds earnings call transcripts, live quotes, options chains with Greeks, guidance and a screener.
Waifu-im/waifu-api
An easy to use API that allows you to get waifu pictures from an archive of over 4000 images and multiple tags!
idaholab/DeepLynx
DeepLynx Nexus is version 2 of the DeepLynx data warehouse, and acts as the central integration point for the DeepLynx data ecosystem. Nexus is a functional data catalog and digital thread tool, allowing users to track metadata from disparate sources and identify ontological and semantic connections between data. It is written in C# and React.
Version Downloads Last Updated
0.3.0 4,832,779 12/21/2025
0.2.2 1,705,902 3/26/2025
0.2.1 916,813 6/26/2024
0.2.0 453,462 11/24/2023
0.2.0-rc.1 7,992 10/14/2023
0.1.2 32,280 9/25/2023
0.1.1 81,437 4/25/2023
0.1.0 10,427 3/30/2023