- It translate API request to SQL queries.
- Send SQL query to database server
- Read resulting database rows
- Translate database rows to API response
3.1. Models
using System;
namespace GameStore.Api.Models;
public class Game
{
public int Id {get; set;}
public required string Name {get; set;}
public Genre? Genre {get; set;}
public int GenreId {get; set;}
public decimal Price {get; set;}
public DateOnly ReleaseDate {get; set;}
}
3.2. GamesStoreContext
using GameStore.Api.Models;
using Microsoft.EntityFrameworkCore;
namespace GameStore.Api.Data;
public class GameStoreContext(DbContextOptions<GameStoreContext> options) : DbContext(options)
{
public DbSet<Game> Games => Set<Game>();
public DbSet<Genre> Genres => Set<Genre>();
}
using GameStore.Api.Data;
using GameStore.Api.Endpoints;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddValidation();
var connString = "Data Souce=GameStore.db";
builder.Services.AddSqlite<GameStoreContext>(connString);
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapGamesEndpoints();
app.Run();
3.3. Dotnet EF
Microsoft.EntityFrameworkCore.Design
// Helps with migration for example