Go to Tools > Nuget Package Manager > Package Manager Console
Adapt and paste the following code. This includes:
- db’s connection string
- name for the context it’s going to create
- Where it pastes the data classes to
Scaffold-DBContext "Host=host_here;Database=database_name;Username=username_here;Password=pwd_here" Npgsql.EntityFrameworkCore.PostgreSQL -DataAnnotations -Context ContextNameHereDbContext -ContextDir Data -o Models/DB -force -NoOnConfiguring -verbose
This creates:
DbContext.cs
class- all Model/Data classes
Remember to modify Startup.cs
to set the service classes as AddTransient
for the services which use this DbContext
services.AddTransient<SomethingService, SomethingService>();
Add into the service, the context you’ve just created and set it at the constructor.
private readonly ApplicationDbContext _context;