A small ASP.NET Core Web API for a book catalogue, built database-first: the table is designed in SQL,
and the C# entity and DbContext are generated from the live database with EF Core scaffolding.
- List all books, sorted by title.
- Get a single book by ID. An unknown ID returns
404 Not Found. - Add a book. The database assigns the ID; the response is
201 Createdwith aLocationheader. - Update a book by ID (
204 No Content), or404if it doesn't exist. - Delete a book by ID (
204 No Content), or404if it doesn't exist. - Reject blank or overlong title/author fields, negative prices/quantities, out-of-range prices and prices with more than two decimal places with
400 Bad Request. - Store prices as
DECIMAL(10, 2), never as floating point.
.NET 10 · ASP.NET Core (controllers) · EF Core 10 with the SQL Server provider · SQL Server 2022 in Docker ·
OpenAPI + Scalar · dotnet user-secrets for the connection string
BookCatalog/
├── database/schema.sql creates the database, the Books table and two sample rows
├── dotnet-tools.json pins the dotnet-ef tool version
└── BookCatalog.Api/
├── Models/ generated by `dotnet ef dbcontext scaffold` (don't edit by hand)
├── Contracts/BookRequest.cs request validation, separate from scaffolded entities
├── Controllers/BooksController.cs
├── BookCatalog.Api.http ready-made requests for the endpoints below
└── Program.cs
The application requires a reachable SQL Server instance and a connection string stored with dotnet user-secrets.
The container commands below require Docker and an x86-64 Linux environment. Microsoft does not support SQL Server containers
under Apple Silicon emulation; --platform linux/amd64 does not remove that limitation.
See SQL Server container requirements.
On an ARM Mac, the API can connect to SQL Server hosted on a supported machine instead.
- Start SQL Server (the password must have 8+ characters with upper case, lower case and digits):
docker run -d --name sql --platform linux/amd64 -e ACCEPT_EULA=Y -e 'MSSQL_SA_PASSWORD=<your-password>' \ -p 127.0.0.1:1433:1433 mcr.microsoft.com/mssql/server:2022-latest - Create the database:
sqlcmd -S localhost -U sa -P '<your-password>' -C -i database/schema.sql - Restore the EF tool and store the connection string outside the repository:
dotnet tool restore cd BookCatalog.Api dotnet user-secrets set "ConnectionStrings:BookCatalog" "Server=localhost,1433;Database=BookCatalog;User Id=sa;Password=<your-password>;TrustServerCertificate=True"
- Run it and open http://localhost:5159/scalar/v1:
dotnet run
Change database/schema.sql (or the database itself), then from BookCatalog.Api:
dotnet ef dbcontext scaffold "Name=ConnectionStrings:BookCatalog" Microsoft.EntityFrameworkCore.SqlServer \
--output-dir Models --context BookCatalogContext --data-annotations --no-onconfiguring --force| Method | Route | Success | Errors |
|---|---|---|---|
| GET | /api/books |
200, list | |
| GET | /api/books/{id} |
200, book | 404 |
| POST | /api/books |
201, book + Location |
400 |
| PUT | /api/books/{id} |
204 | 400, 404 |
| DELETE | /api/books/{id} |
204 | 404 |
dotnet test BookCatalog.slnxThe tests use ASP.NET Core's test host and EF Core's in-memory provider; SQL Server is not needed to run them. They verify HTTP routing, validation and application behavior. They do not verify SQL Server's SQL execution, relational constraints or concurrency behavior. Tests cover decimal values, database-assigned IDs and CRUD responses.
Manual acceptance checks, run against a freshly created database. Each request is in BookCatalog.Api.http.
| # | Check | Steps | Expected result |
|---|---|---|---|
| 1 | List | GET /api/books |
200; the two sample books, sorted by title |
| 2 | Get one | GET /api/books/1 |
200; The Pragmatic Programmer |
| 3 | Unknown ID | GET /api/books/999 |
404 |
| 4 | Add | POST /api/books with a complete book |
201; the Location header ends in /api/books/3; body has "id": 3 |
| 5 | Validation | POST /api/books without author |
400; errors.Author explains the problem |
| 6 | Update | PUT /api/books/1 with a new price, then GET /api/books/1 |
204, then 200 showing the new price |
| 7 | Delete | DELETE /api/books/3, then GET /api/books/3 |
204, then 404 |
| 8 | Data check | sqlcmd ... -d BookCatalog -Q "SELECT * FROM Books" |
Rows match what the API returns |
Inspired by manish07/CrudWithWebApiDbFirst. This is an independent rewrite, not a copy of that code.