January 7th, 2026
0 reactions

Secure and Intelligent: Queryable Encryption and Vector Search in MongoDB EF Core Provider

This is a guest post by Rishit Bhatia and Luce Carter. Rishit is a Senior Product Manager at MongoDB focusing on the .NET Developer Experience and worked hands-on with C# for many years before moving into Product Management. Luce is a Senior Developer Advocate at MongoDB, Microsoft MVP, and lover of code, sunshine, and learning. This blog was reviewed by the Microsoft .NET team for EF Core.*

The MongoDB Entity Framework (EF) Core provider has been generally available since May 2024. Since its launch, we’ve been thrilled by the positive reception from the .NET developer community and its growing adoption across a wide range of applications.

The provider empowers developers to integrate MongoDB seamlessly into their .NET projects using EF Core features like LINQ queries, change tracking, and optimistic concurrency, while harnessing MongoDB’s flexibility and scalability.

We’ve continued to enhance the provider by adding new capabilities, such as improved support for MongoDB-specific operations, performance optimizations, and expanded compatibility with the latest EF versions. These updates reflect our commitment to providing developers with a robust, intuitive, and reliable way to build modern applications with MongoDB and EF Core.

We’re excited to share two new major feature additions-namely, Queryable Encryption and Vector Search! These let you leverage some marquee MongoDB capabilities directly from the MongoDB EF Core provider through convenient APIs without having to rely on the underlying .NET C# driver.

Let’s take a sneak peek at what these look like.

MongoDB Queryable Encryption

Queryable Encryption is a groundbreaking feature that allows you to encrypt sensitive data in your MongoDB database while still enabling you to query that data. This means you can maintain strong data privacy and compliance without sacrificing the ability to perform your queries.

It’s particularly important for industries with strict data governance requirements, like healthcare and finance.

For users, this translates to enhanced security, reduced risk of data breaches, and simplified compliance efforts, as they can confidently encrypt data at rest and in transit without needing to re-architect their applications for querying encrypted fields.

Configuring your data model to define encrypted properties in your DbContext’s OnModelCreating is as simple as seen below:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Employee>(entity =>
    {
        entity.Property(e => e.TaxPayerId)
            .IsEncryptedForEquality(<Your Data Encryption Key GUID>));

        entity.Property(e => e.Salary)
            .HasBsonRepresentation(BsonType.Decimal128)
            // Salaries from 0 to 10 million, no decimal place precision
            .IsEncryptedForRange(0m, 10000000m, 0,
                <Your Data Encryption Key GUID>));              
    });
}

A small snippet of how querying works for equality and range queries is shown below:

//Encrypted Equality Query
var specificEmployee = db.Employees.Where(e => e.TaxPayerId == "45678");

//Encrypted Range Query
var seniorEmployees = db.Employees.Where(e => e.Salary >= 100000m && e.Salary < 200000m);

The full tutorial shows you how you can encrypt your data and query it using the MongoDB EF Core provider.

MongoDB Vector Search

Vector search revolutionizes how you can find and analyze unstructured data by enabling similarity searches based on semantic meaning rather than exact keywords. This capability is powered by vector embeddings, which represent data points (like text, images, or audio) as numerical vectors in a high-dimensional space.

The importance of vector search lies in its ability to unlock new possibilities for AI-powered applications, such as recommendation engines, semantic search, and anomaly detection.

For users, this means more intelligent and relevant search results, the ability to build sophisticated AI applications directly on their MongoDB data, and a richer understanding of their unstructured information.

The following code block shows how you can easily set up your data model with vector embedding fields either in your OnModelCreating function or in your data model class itself:

//Inside the OnModelCreating function 
b.Property(e => e.PlotEmbedding)
   .HasElementName("plot_embedding_voyage_3_large")
   .HasBinaryVectorDataType(BinaryVectorDataType.Float32);

// OR In the data model definition for the property
[BinaryVector(BinaryVectorDataType.Float32)]
public float[] ? PlotEmbedding {get; set;}

Once you’ve configured the above and have appropriate vector indexes to run vector search queries, running queries with your data is as easy as seen below:

//Simple LINQ Vector Search query
var similarMovies = await db.Movies.VectorSearch(
        e => e.PlotEmbedding,
        myCustom.PlotEmbedding,
        limit: 10)
    .ToListAsync();

There are many more capabilities-such as running vector search queries with prefilters and projecting its scores available-in our full tutorial.

Get started today

The MongoDB EF Core provider now allows you to use marquee MongoDB capabilities like Queryable Encryption and Vector Search directly from the provider.

To get started, you can create a simple .NET console app that connects to MongoDB Atlas, MongoDB’s fully managed cloud database service. From there, you can explore these powerful capabilities in your application using the MongoDB EF Core provider. For step-by-step instructions, check out the quickstart guide.

Learn more

To learn more about EF Core and MongoDB:

Go ahead and give it a go! Plus, if you want to level up your MongoDB skills and get a Credly-backed badge you can show off on LinkedIn, why not try the Vector Search badge.

0 comments