> ## Content Index
> Fetch the complete content index at: https://blog.tsd.digital/llms.txt
> Use this file to discover other available public pages before exploring further.

# Setting up EF Core 9.0 Cosmos Provider and .NET Aspire 9.4
- URL: https://blog.tsd.digital/setting-up-ef-core-9-0-cosmos-provider-and-net-aspire-9-4/
- Published: 2025-09-02T07:30:36.000Z
- Updated: 2025-09-02T08:00:26.000Z
- Description: Azure Cosmos DB delivers global distribution and lightning-fast performance for modern cloud apps. Combined with .NET Aspire and EF Core, you get familiar development patterns with cloud-native power - no complex SDKs required.
- Author: Robin Davis
- Tags: Aspire, .Net, Cosmos, Azure

Modern cloud apps need databases that work across the globe with lightning-fast response times and rock-solid reliability. Azure Cosmos DB ticks all these boxes. If you're building with .NET Aspire (Microsoft's framework for cloud-native applications) you can integrate Cosmos DB with Entity Framework Core (EF Core) without breaking a sweat.

Lets examine how to utilise the Cosmos DB provider for EF Core in a .NET Aspire project, explore some of Cosmos's unique features, and see how Aspire simplifies the setup process.

**Why Use Aspire, EF Core, and Cosmos DB Together?**

**Aspire handles the heavy lifting** – It manages configuration, environment variables, secrets, and service dependencies (like Cosmos DB) so you don't have to.

**Stick with what you know** – You can use familiar LINQ queries and DbContext patterns instead of learning a completely new SDK from scratch.

**Built for the cloud** – Aspire was designed for distributed workloads, and Cosmos DB is built for exactly that kind of environment.

### Setting Up Cosmos DB in a .NET Aspire Project

In your Aspire AppHost Project install the Aspire.Hosting.Azure.CosmosDB nuget package

```cmd
dotnet add package Aspire.Hosting.Azure.CosmosDB
```

Install the following packages into your API project

```cmd
dotnet add package Aspire.Microsoft.Azure.Cosmos
dotnet add package Aspire.Microsoft.EntityFrameworkCore.Cosmos
```

Configure the aspire resources so your API project can talk to CosmosDB (This is set up for development using the emulator rather than connecting to a real database)

```csharp
#pragma warning disable ASPIRECOSMOSDB001
var builder = DistributedApplication.CreateBuilder(args);

var cosmos = builder.AddAzureCosmosDB("cosmos")
    .RunAsPreviewEmulator(options =>
    {
        options.WithLifetime(ContainerLifetime.Persistent);
        options.WithDataVolume();
        options.WithDataExplorer();
    }).AddCosmosDatabase("MyCosmosDatabase");

#pragma warning restore ASPIRECOSMOSDB001

builder.AddProject<Projects.MyApi>("myapi")
    .WithReference(cosmos);

builder.Build().Run();
```

Finally in your project configure your DB Context to use the cosmo db connection

```csharp
builder.AddCosmosDbContext<BloggingContext>("MyCosmosDatabase", "MyCosmosDatabase");
```

### Key Considerations

- Partition Keys – Cosmos DB uses partitioning for scale. EF Core supports defining partition keys via Fluent API:

```csharp
modelBuilder.Entity<Post>()
    .ToContainer("Posts")
    .HasPartitionKey(p => p.Title);
```

- No Joins – Unlike SQL databases, Cosmos doesn’t support cross-container joins. Consider embedding related data (e.g., Posts within Blog).
- Local Development – Aspire + the Cosmos emulator makes local dev frictionless. You won’t need to spin up real Azure resources until deployment.

### Example Query with EF Core

```csharp
using var context = new BloggingContext();

var recentPosts = await context.Posts
    .Where(p => p.Title.Contains("EF Core"))
    .OrderByDescending(p => p.Id)
    .ToListAsync();
```

### When Should You Use Aspire + EF Core + Cosmos?

- If you’re building cloud-native apps with Aspire and want Cosmos DB as your backing store.
- If your team is comfortable with EF Core and LINQ but new to Cosmos DB.
- If you want local dev simplicity, Aspire + the Cosmos emulator makes setup nearly zero-config.

For advanced scenarios (change feed processing, multi-region writes, fine-grained indexing), you may still prefer using the Cosmos SDK directly. But Aspire + EF Core is a powerful combo for most application data needs.

### Final Thoughts

Using Azure Cosmos DB with EF Core inside .NET Aspire gives you the best of all worlds:

- A familiar programming model via EF Core.
- Cloud-native orchestration and local dev support via Aspire.
- Scalability and global distribution from Cosmos DB.

If you’re starting a new distributed app on Azure, Aspire + EF Core + Cosmos DB is one of the fastest ways to be productive while still building on modern, cloud-ready foundations.