Entity Framework Core, approaches and deployment flow - JU-DEV-Bootcamps/ERAS GitHub Wiki

Entity Framework Core

Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology.

EF Core can serve as an object-relational mapper (O/RM), which: Enables .NET developers to work with a database using .NET objects. Eliminates the need for most of the data-access code that typically needs to be written.

There different approaches when workin with this framework:

Code First:

The Entity Framework Code First approach is the most popular method for implementing Entity Framework in .NET C# applications. In this approach, developers define database entities in code first, rather than directly working with a database. Entity Framework then generates the database and tables based on these entity classes.

Steps

  • Define Classes: Create domain classes representing entities.
  • Configure Relationships: Use Fluent API or Data Annotations to configure relationships between entities.
  • Generate Database: Use migrations to generate and update the database based on the model.

There are no manual changes to the database as all modifications are handled through code. Data annotation attributes are available to define foreign keys, constraints, indexes, and more in the C# code. Since the database is always created or updated from the code, mismatches between the code and database (like column types, table names, etc.) are minimized

Database First:

The "Database First" approach was commonly used before Entity Framework, where the database is designed first, and the application is then developed to interact with it. This approach is typically used when there is an existing database, and you want to build a new application around it, such as when migrating legacy applications to newer technologies.

  • Create Database: Design and create the database using SQL Server Management Studio or any other database tool.
  • Generate Model: Use the Entity Data Model Wizard to generate the model from the existing database.
  • Use Generated Classes: Utilize the auto-generated classes for database operations.

Model First:

In the "Model First" approach, you create the model first using the Entity Framework designer. This model is defined in an EDMX file (.edmx), which represents the database entities. The EDMX file can be used to generate the database schema and auto-generate C# classes for interacting with the database.

Due to the project is new, there's no database created and it's easier to model a database throw the entities definition we're using Code First approach.

Entity Framework Core Migrations and Docker

Here’s how they complement each other:

  • Consistent Environments: Docker ensures a consistent development, testing, and production environment. EF Core Migrations, when encapsulated within a Docker container, guarantee that the database schema matches the application code in any environment.
  • Containerized Database Migrations: Developers can package their applications, including EF Core Migrations, into Docker containers. This containerized approach simplifies the deployment of both the application and its associated database changes.
  • Scalability and Versioned Deployments: Docker allows for easy scaling of applications, and when coupled with EF Core Migrations, developers can ensure versioned deployments, managing database changes seamlessly as they roll out updates.

Migrations in Docker

Our goal is not to guide you through configuring EF Core in a .NET solution (plenty of content is available on that). Instead, our focus is on the Docker side. Here, we’ll outline three key files: docker-compose.yaml to define service containers, Dockerfile for building/running migrations, and optionally, a Makefile to streamline commands.

In the docker-compose.yaml, we define two services: the database, and the migrations. The migrations service waits for the database service, executes the steps outlined in the Dockerfile, and initiates a container to run migrations, facilitating the creation/update of the SQL Server database.