EF Core 8 Features, Code First Approach, Migrations, Entity Configurations in .NET 8 Microservices

Mehmet Ozkaya
4 min readMar 27, 2024


We are going to learn develop Ordering.Infrastructure Layer with EF Core Code First Approach and SQL Server Connection.

I have just published course — .NET 8 Microservices: C# 12, DDD, CQRS, Vertical/Clean Architecture.

In the landscape of modern application development, Entity Framework Core 8 (EF Core 8) stands out as a pivotal tool, especially when implemented within a Domain-Driven Design (DDD) architecture in microservices such as an Ordering system. This article delves into the intricacies of utilizing EF Core 8’s capabilities, from establishing a database context to intricately mapping domain objects and automating migrations.

🚀 EF Core 8 Features and the Code-First Paradigm

EF Core 8 introduces a plethora of enhancements that streamline database operations, making it an ideal choice for developers aiming for efficiency and precision. Adopting the Code-First approach, developers can focus on defining their domain models in C#, allowing EF Core to handle the database schema generation. This method not only accelerates development cycles but also ensures that the database design remains in sync with the domain model.

Folder Structure of Infrastructure Layer

🛠️ Crafting the EF Core DBContext for Entity Management

The heart of EF Core’s interaction with the database lies within the DbContext object. In our Ordering microservice, developing a custom DbContext tailored to our domain entities is crucial. This context serves as the conduit through which our entities, such as Orders and OrderItems, interact with the underlying SQL Server database, thus facilitating CRUD operations seamlessly.

Entity Framework Core NuGet Packages for DB Providers

🗺️ From Domain Models to EF Entities: The Mapping Conundrum

Mapping domain objects to EF entities is a nuanced process that requires careful consideration. It involves translating the rich behaviors and properties of domain entities into a structure that EF Core can comprehend and manipulate. This translation is pivotal in ensuring that the domain’s intricacies are accurately represented and persisted in the database.

Mapping Order Entity fields into Order Configuration

🔧 Configuring Entities with Fluent API: The Art of ModelBuilder

EF Core’s Fluent API, particularly the ModelBuilder, offers a robust mechanism to configure entity mappings and relationships. Through detailed configurations, we can dictate how our domain entities are mapped to the database schema, including table names, column types, constraints, and relationships. This explicit configuration enhances clarity and precision in how our domain is represented in the database.

🌱 Seamless Migrations and Seeding: Automating Database Evolution

One of EF Core’s standout features is its migration capabilities, which automate the process of evolving the database schema over time. Coupled with the ability to seed initial data, this feature ensures that our database not only matches our current domain model but is also populated with essential data upon application startup. This automation significantly reduces manual intervention and potential for error.

Create Database w/ Migrate method

📢 Orchestrating Domain Events with EF Core and MediatR

In a DDD context, raising and dispatching domain events is vital for decoupling components and ensuring reactive system behavior. Integrating EF Core with MediatR allows us to tap into the lifecycle of entity changes, particularly through interceptors like SaveChangesInterceptor, to dispatch domain events effectively. This integration is a powerful mechanism for triggering domain logic in response to data persistence events.

Dispatch DomainEvents w/ EF Core Interceptor

📚 Conclusion: Mastering EF Core 8 in Microservices

The journey through EF Core 8, from establishing a robust DbContext to automating migrations and integrating domain events, underscores its indispensable role in modern microservice architectures. By meticulously mapping our domain objects, configuring entities, and leveraging EF Core's advanced features, we fortify the foundation of our Ordering microservice, making it more resilient, maintainable, and aligned with our domain's complexities. Embrace these strategies to elevate your microservices to new heights of efficiency and domain fidelity.

I have just published course — .NET 8 Microservices: C# 12, DDD, CQRS, Vertical/Clean Architecture.

This is step-by-step development of reference microservices architecture that include microservices on .NET platforms which used ASP.NET Web API, Docker, RabbitMQ, MassTransit, Grpc, Yarp API Gateway, PostgreSQL, Redis, SQLite, SqlServer, Marten, Entity Framework Core, CQRS, MediatR, DDD, Vertical and Clean Architecture implementation with using latest features of .NET 8 and C# 12.



Mehmet Ozkaya

Software Architect | Udemy Instructor | AWS Community Builder | Cloud-Native and Serverless Event-driven Microservices https://github.com/mehmetozkaya