DB First Code Generation
DB First Code Generation
1. Generate Models from the DB at the Package Manager Console:
Scaffold-DbContext "YourConnectionStringHere" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
or this at command line:
dotnet ef dbcontext scaffold XXXXXXXXXXXXXXXXXXX
Parameters:
-Tables // Allows explicit list of specific tables to scaffold
2. Generate the Full CRUD Controller & Views:
dotnet scaffold controller -name ProductsController -m ProductModel -dc MyDbContext --relativeFolderPath Controllers --useDefaultLayout
// a) API Controller with Actions, using EF
dotnet scaffold controller -name ProductsController -api -async -m Product -dc MyDbContext -outDir Controllers
// Key Flags: Requires -api (to skip HTML views), -m [ModelName], and -dc [DbContextName]. Adding -async creates asynchronous task-based methods
// b) API Controller with Read/Write Actions with no data access code or DB
// dependencies
dotnet scaffold controller -name InventoryController -api -actions -outDir Controllers
// Key Flags: (or --readWriteActions) switch
// c) Empty API Controller
dotnet scaffold controller -name CustomController -api -outDir Controllers
Generated Output Type -api -m -dc -actions
1) EF CRUD Controller Required Required Required Omits
2. Read/Write Mock C Required Omits Omits Required
3. Empty API Controller Required Omits Omits Omits
--------------------------------------------------------
//(Note: If you entirely omit the -api flag, the engine defaults to creating MVC
// Controllers with Razor HTML views instead of web API endpoints).
--------------------------------------------------------
// Alternative: Minimal API Scaffolding
// In newer versions of .NET, if you prefer low-ceremony Minimal APIs over
// controllers, you can bypass the controller command entirely:
dotnet scaffold minimalapi -name ProductEndpoints -m Product -dc MyDbContext
// This generates an Endpoints extension class that registers map routes directly
// to app.MapGet and app.MapPost inside your application pipeline
================================================================
1. Minimal API Scaffolding (minimalapi)
Instead of full controller classes, this bypasses the standard routing
architecture to generate light, high-performance endpoints directly mapped inside
your application pipeline.
Operation: dotnet aspnet-codegenerator minimalapi
What it builds: An endpoint extension class containing direct app.MapGet,
app.MapPost, app.MapPut, and app.MapDelete route handlers mapped to your EF models.
2. Security & Authentication Scaffolding (identity & blazor-identity)
These commands generate all the necessary backend code, database schemas, and
frontend interfaces required to manage user logins, registrations, and account
settings.
Operations:
dotnet aspnet-codegenerator identity (For MVC, Razor Pages, or Web API backend
authentication)
dotnet aspnet-codegenerator blazor-identity (Tailored specifically to Blazor
Web Apps using interactive render modes).
What it builds: Registers identity service dependencies, maps security DBs, and
exports dozens of customizable account pages (Login, Register, Two-Factor Auth,
Reset Password).
3. Blazor UI Scaffolding (blazor)
This operation builds full-stack frontend interfaces for interactive web
applications.
Operation: dotnet aspnet-codegenerator blazor
What it builds: A set of active .razor component pages including Create.razor,
Delete.razor, Details.razor, Edit.razor, and an Index.razor page equipped with
responsive data grids and HTML form validation.
4. Razor Pages Scaffolding (razorpage)
This option targets page-focused web application architectures using standard
server-side rendering.
Operation: dotnet aspnet-codegenerator razorpage
What it builds: Pairs of .cshtml views alongside corresponding .cshtml.cs
PageModel files. It automatically handles bind properties and maps user web actions
directly to EF CRUD DB functions.
5. Standard MVC Views (view)
If you already have an MVC controller but lack the presentation layer, you can
scaffold individual views independently.
Operation: dotnet aspnet-codegenerator view
What it builds: A standard .cshtml razor view template using preset layout
styles like List, Create, Edit, Delete, Details, or a completely Empty slate.
6. Multi-Tenant Organization (area)
Used for managing massive codebases by partitioning your app into separate,
miniature logical modules.
Operation: dotnet aspnet-codegenerator area
What it builds: A structured directory folder pattern containing distinct
localized Controllers, Models, and Views subdirectories to segregate features
(e.g., separating an /Admin workspace from a /Customer dashboard).
Comments
Post a Comment