Entity Framework like ORM for Cosmos DB

Cosmos DB now supports Entity Framework (announcement) (although at this point at the preview state); but works quite nicely.

p.s. nice 3rd party guide with more details on how to set things up:

However, you should really think whether you need EF (ORM). It is probably useful if you are switching you db to CosmosDB; and don't want to change your EF code right away; note though that in any case switching dbs requires work on EF-front; even more so if switching db types: relational to NoSQL.

EF does ORM (object to db mapping); however NoSQL already has object mapping so EF is not really needed (actually unnecessarily complicates things.

There is a nice write up from Microsoft on the related topics: https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/nosql-database-persistence-infrastructure


Cosmonaut is exactly what you're looking for.

It is a simple and minimalistic object mapper, which creates a collection-to-object relationship. You can use your POCO objects and do all the CRUD operations.

The idea is pretty simple. You can have one CosmosStore per entity (POCO/dtos etc). This entity will be used to create a collection or use part of a one in Cosmos DB and it will offer all the data access for this object. It is optimized for performance out of the box and takes a lot of efficient decisions for you.

It supports collection sharing in order to reduce the cost of having multiple objects in one collection as well.

Read more about Cosmonaut here.

Disclaimer, I am the creator of Cosmonaut.


There is no recommended ORM framework available for CosmosDB, there are few ORM libraries out there which you could try,

(i) Cosmonaut

(ii) CosmicGraph

Update:

ORMs are there in general to solve the problem of mapping objects to relational databases. Document database like CosmosDB, you can store plain objects as JSON and not have to worry about the constraints of a relational database.

However, CosmosDB team provided support to it, The provider works like any other EF Core provider. You reference its package in your project and then specify it in OnConfiguring or if you’re using ASP.NET Core when defining the DbContext in Startup.

The provider is named Microsoft.EntityFrameworkCore.Cosmos.You can add it with the command,

dotnet add package Microsoft.EntityFrameworkCore.Cosmos