Skip to content

efcore/EFCore.SqlServer.VectorSearch

Repository files navigation

EFCore.SqlServer.VectorSearch

Important

This plugin is in prerelease status, and the APIs described below are likely to change before the final release. Vector Functions are in Public Preview. Learn the details about vectors in Azure SQL here: https://aka.ms/azure-sql-vector-public-preview

This Entity Framework Core plugin provides integration between EF and Vector Support in Azure SQL Database, allowing LINQ to be used to perform vector similarity search, and seamless insertion/retrieval of vector data.

To use the plugin, reference the EFCore.SqlServer.VectorSearch nuget package, and enable the plugin by adding UseVectorSearch() to your UseSqlServer() config as follows:

builder.Services.AddDbContext<ProductContext>(options =>
  options.UseSqlServer("<connection string>", o => o.UseVectorSearch()));

Once the plugin has been enabled, add an ordinary float[] property to the .NET type being mapped with EF:

public class Product
{
    public int Id { get; set; }
    public float[] Embedding { get; set; }
}

Finally, configure the property to be mapped as a vector by letting EF Core know using the HasColumnType method. Use the vector type and specify the number of dimension that your vector will have:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>().Property(p => p.Embedding).HasColumnType("vector(3)");
}

That's it - you can now perform similarity search in LINQ queries! For example, to get the top 5 most similar products:

var someVector = new[] { 1f, 2f, 3f };
var products = await context.Products
    .OrderBy(p => EF.Functions.VectorDistance("cosine", p.Embedding, vector))
    .Take(5)
    .ToArrayAsync();

A full sample using EF Core and vectors is available here:

https://github.com/Azure-Samples/azure-sql-db-vector-search/tree/main/EF-Core

Ideas? Issues? Let us know on the issues page.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages