cosmos-db
  1. cosmos-db-data-modeling-with-entity-framework-core

Data Modeling with Entity Framework Core

Entity Framework Core is a popular ORM (Object Relational Mapping) tool that enables .NET developers to work with databases using strongly-typed entities that map to database tables. This tutorial will focus on using Entity Framework Core with CosmosDB for data modeling.

Syntax

To create an entity class, use the following syntax:

public class MyEntity
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

To establish a connection to CosmosDB for use with Entity Framework Core, use the following syntax:

services.AddDbContext<MyDbContext>(options =>
    options.UseCosmos(
        "{connection-string}",
        "{database-name}",
        "{container-name}"
    ));

Example

public class Person
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

public class MyDbContext : DbContext
{
    public DbSet<Person> Persons { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseCosmos(
            "AccountEndpoint=https://mycosmosaccount.documents.azure.com:443/;AccountKey=myaccountkey;",
            "MyDatabase",
            "MyContainer"
        );
    }
}

Output

Entity Framework Core will automatically map the Person entity to the specified CosmosDB container, allowing you to easily read and write data to the database.

Explanation

When using Entity Framework Core with CosmosDB, it's important to keep in mind that the database operates in a non-relational, document-based paradigm. This means that the entities in your .NET code should be modeled with this in mind, using properties and attributes that map to the data structure of your CosmosDB container.

In the example above, we created a Person entity class with properties for Id, FirstName, LastName, and Age. We then created a DbContext class that specifies the database connection string and the name of the CosmosDB database and container to use.

Use

Entity Framework Core with CosmosDB can be used to efficiently read and write data to a non-relational database using a familiar code-first approach. This can be useful in a variety of scenarios, including web applications, mobile apps, and cloud services that require scalability and performance.

Important Points

  • Entity Framework Core allows .NET developers to work with databases using strongly-typed entities that map to database tables.
  • CosmosDB is a non-relational document-based database that uses JSON documents to store data.
  • When using Entity Framework Core with CosmosDB, it's important to model your entities with the non-relational data structure of the database in mind.

Summary

In this tutorial, we learned about using Entity Framework Core with CosmosDB to model data in a non-relational, document-based database. We looked at the syntax for creating entities, establishing a database connection, and performing CRUD operations on the database. Finally, we discussed some important points to keep in mind when working with these two technologies together.

Published on: