Data

A modular data model in EF Core: one DbContext from many modules

Habr C# (ru) gandjustas
Data

Instead of giving each module its own database or DbContext, a modular monolith can assemble one EF Core model from every module's IEntityTypeConfiguration classes. The author compares four data layouts on .NET 10 and shows that only the single-model option keeps SQL JOINs and one SaveChanges across modules. In his benchmark the database-side join served 12% more requests than the HTTP-based one.

Key points

  • Four layouts exist: a database per module, a DbContext per module in a shared database, replicated data, or one model assembled from modules.
  • Only the single-model approach gives SQL joins, an atomic SaveChanges across modules and one migration history.
  • The model is built with ApplyConfigurationsFromAssembly over the loaded module assemblies; ModuleBase records each module in a Modules configuration section.
  • Module endpoints depend on DbContext and reach entities via Set<T>(), so DbSet properties and navigation properties are off the table.
  • Relationships such as foreign keys live in a separate composing module that must be loaded last.
  • Benchmark: 2011 vs 1796 requests per second, p95 33.5 ms vs 43.5 ms, and twice as much code in the HTTP-join variant.

Why it matters

A database joins better than a foreach loop in the application: the single model means less code and more throughput, at the cost of turning every cross-module reference into a hard constraint on which topologies can start.

Real-world use

  • A SaaS application whose ordering, customer and billing modules run in one process and whose reports need SQL joins across them.
  • A team migrating gradually by running the same image as a monolith or as microservices, switching topology through HOSTINGSTARTUPASSEMBLIES.
  • Replacing network-based data stitching with in-database joins while moving away from an existing microservice split.
  • A platform team that wants module boundaries visible in code but a single schema and a single migration history.

This summary was generated by the agent from the original post on Habr C# (ru). The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message