Jul 8, 2024
dotnet --version
dotnet --info
dotnet new webapi -o GameStoreApiappsettings.json.
{
"ConnectionStrings": {
"GameStore": "Data Source=GameStore.db"
}
}
GameStoreContext)
builder.Services.AddDbContext<GameStoreContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("GameStore")));
dotnet ef migrations add InitialCreate
dotnet ef database update
Required and StringLength annotations.app.MapGet("/games", async (GameStoreContext db) => await db.Games.ToListAsync());
app.MapGet("/games/{id}", async (GameStoreContext db, int id) => await db.Games.FindAsync(id));
app.MapPost("/games", async (GameStoreContext db, CreateGameDto newGame) => {...};
app.MapPut("/games/{id}", async (GameStoreContext db, int id, UpdateGameDto updatedGame) => {...};
app.MapDelete("/games/{id}", async (GameStoreContext db, int id) => {...}
async and await for database operations.app.MapGet("/games", async (GameStoreContext db) => await db.Games.AsNoTracking().ToListAsync());
public record GenreDto(int Id, string Name);
app.MapGet("/genres", async (GameStoreContext db) => await db.Genres.AsNoTracking().ToListAsync());
appsettings.json, read using DI configuration.