Tutorials Microservices with .NET
Implementing User Microservice Infrastructure Layer — Complete Guide
Implementing User Microservice Infrastructure Layer — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Microservices with .NET on Toolliyo Academy.
On this page
Microservices with .NET · Lesson 14 of 131
Implementing User Microservice Infrastructure Layer
Beginner → Intermediate → Advanced → Professional
Beginner · 1 — Foundations · ~6 min · Module 2: Building User Microservice
What is this?
Infrastructure implements IUserRepository with EF Core, configures UserDbContext, and runs SQL migrations against ShopNest_Users.
Why should you care?
Swapping SQL Server for PostgreSQL later only changes Infrastructure — Domain stays untouched.
See it live — copy this example
Create a Web API project (dotnet new webapi), paste the code, then run dotnet run.
public class UserDbContext : DbContext
{
public UserDbContext(DbContextOptions<UserDbContext> o) : base(o) { }
public DbSet<UserProfile> Users => Set<UserProfile>();
}
public class UserRepository : IUserRepository
{
private readonly UserDbContext _db;
public UserRepository(UserDbContext db) => _db = db;
public Task AddAsync(UserProfile user, CancellationToken ct = default)
{
_db.Users.Add(user);
return _db.SaveChangesAsync(ct);
}
}
Run Example »
Edit the code and click Run — like W3Schools Try it Yourself.
What happened?
- EF configuration for Email value object conversion can live in OnModelCreating.
- Migrations live in Infrastructure project.
Try it yourself
- Add Microsoft.EntityFrameworkCore.SqlServer package.
- build UserRepository.
- dotnet ef migrations add Initial -p Infrastructure -s Api
- Change a string or route in the example and save — watch Swagger or the RabbitMQ Management UI update.
- Break the code on purpose (remove a semicolon), read the error message, then fix it.
Remember
Infrastructure = EF + repository impl. One DbContext per service. Migrations version schema.