Exercise - Reverse engineering
ContosoPizza's manager has asked you to add an endpoint to list coupons from an existing database. In this unit, you'll create scaffolding from an existing database and modify the resulting entity class.
Inspect promotions database
- In the EXPLORER pane, right-click on Promotions/Promotions.db file and select Open Database.
- In the SQLITE EXPLORER pane, expand the Promotions.db and Coupons nodes. Note the data schema.
- Right-click the Coupons node and select Show table. Inspect the coupon data.
Scaffold the promotions context and coupon model
Run the following command:
dotnet ef dbcontext scaffold "Data Source=.\Promotions\Promotions.db" Microsoft.EntityFrameworkCore.Sqlite --context-dir .\Data --output-dir .\ModelsThe preceding command:
- Scaffolds a
DbContextand model classes using the provided connection string. - Specifies the
Microsoft.EntityFrameworkCore.Sqlitedatabase provider should be used. - Specifies directories for the resulting
DbContextand model classes.
- Scaffolds a
Open Models\Coupon.cs. The
Expirationproperty is defined as a string, since SQLite doesn't have a datetime type. ChangeExpiration's type from astring?to aDateTime. EF Core will manage the conversion of datetime to string data.using System; using System.Collections.Generic; namespace ContosoPizza.Models { public partial class Coupon { public long Id { get; set; } public string Description { get; set; } = null!; public DateTime Expiration { get; set; } } }Tip
New scaffolded files can be generated if the database changes. The generated files are overwritten each time, but are created as
partialclasses you can extend them with custom properties and behaviors in your own, separate files.
Add coupon endpoint
In the Controllers folder, add a file named CouponController.cs containing the following code:
using ContosoPizza.Data; using ContosoPizza.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace ContosoPizza.Controllers; [ApiController] [Route("[controller]")] public class CouponController : ControllerBase { PromotionsContext _context; public CouponController(PromotionsContext context) { _context = context; } [HttpGet] public IEnumerable<Coupon> Get() { return _context.Coupons .AsNoTracking() .ToList(); } }In the preceding code:
- A
PromotionsContextis injected into the constructor. - The
Getmethod returns all the coupons.
- A
In Program.cs, replace the
// Add the PromotionsContextcomment with the following code:builder.Services.AddSqlite<PromotionsContext>("Data Source=./Promotions/Promotions.db");The preceding registers
PromotionsContextwith the dependency injection system.Save all your changes and run the app.
dotnet run --urls=https://localhost:5101
Test the endpoint
In another terminal, run the HttpRepl command.
httprepl https://localhost:5101Switch to the
Couponendpoint.cd CouponRetrieve the coupons from the database.
getNote
expirationis a datetime.
That's it! You've created and modified scaffolding from an existing database!
Need help? See our troubleshooting guide or provide specific feedback by reporting an issue.