Skip to content

Instantly share code, notes, and snippets.

@joe-oli
Last active July 22, 2023 16:18
Show Gist options
  • Save joe-oli/469204e76f1df4df73f8cd49920f2521 to your computer and use it in GitHub Desktop.
Save joe-oli/469204e76f1df4df73f8cd49920f2521 to your computer and use it in GitHub Desktop.
Entity-Framework with DOTNETCORE Scaffolding
I am annoyed as fck there is no point-and-click UI to do this;
Hence these notes as a reminder;
FIRST INSTALL via NuGet in YOUR project (NB: you may have multiple projects in the solution!)
a) 'Microsoft.EntityFrameworkCore.Tools'; choose same Framework version that your project will target;
b) 'Microsoft.EntityFrameworkCore.SqlServer'; choose the PROVIDER you want to use in the Scaffold-DbContext command.
++++++++
0) Open the (Nuget) package manager console: Tools /NuGet PM / PM Console
1) **EXAMPLE**: (all on one line)
Scaffold-DbContext "Server=MyServerName\SQLExpress;Database=MyProjDB;User Id=dev_user1;Password=dev_user1;MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer
-Namespace "WebApi.ModelTemp" -Context "EntitiesCtx" -DataAnnotations -OutputDir ModelTemp
-DataAnnotations creates classes with Attributes; (the classic style of .NET Fx v4.x
if this is ommitted, it will use the Fluid API style, which I think is total crap, because you cannot see at a glance field properties;
e.g. field-sizes (specially max string lengths), PK's, FK's (you will have to go hunt around for that info, pathetic really to the clever smartarse who devised this Fluid BS)
-Namespace is used, because by default it uses the project name (in my case, my Proj name had underscores, e.g. My-Project-WebApi.prj;
So the scaffolding cmd tries to create a namespace as My-Project-WebApi.ModelTemp WHICH IS illegal/not valid !
-Context is used so I can control the dbCtx object name, I don't want it to follow MyDatabaseNameDB -> MyDatabaseNameDBContext
2) For help, see the next command;
In general, type 'get-help NuGet' to see all available NuGet commands (where NuGet is the Powershell NuGet module)
PM> get-help scaffold-dbcontext –detailed
Do you want to run Update-Help?
The Update-Help cmdlet downloads the most current Help files for Windows PowerShell modules, and installs them on your computer. For more information about the Update-Help cmdlet, see https:/go.microsoft.com/fwlink/?LinkId=210614.
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):y
NAME
Scaffold-DbContext
SYNOPSIS
Scaffolds a DbContext and entity types for a database.
SYNTAX
Scaffold-DbContext [-Connection] <String> [-Provider] <String> [-OutputDir <String>] [-ContextDir <String>] [-Context <String>] [-Schemas
<String[]>] [-Tables <String[]>] [-DataAnnotations] [-UseDatabaseNames] [-Force] [-NoOnConfiguring] [-Project <String>] [-StartupProject
<String>] [-Namespace <String>] [-ContextNamespace <String>] [-NoPluralize] [-Args <String>] [<CommonParameters>]
DESCRIPTION
Scaffolds a DbContext and entity types for a database.
PARAMETERS
-Connection <String>
The connection string to the database.
-Provider <String>
The provider to use. (E.g. Microsoft.EntityFrameworkCore.SqlServer)
-OutputDir <String>
The directory to put files in. Paths are relative to the project directory.
-ContextDir <String>
The directory to put the DbContext file in. Paths are relative to the project directory.
-Context <String>
The name of the DbContext. Defaults to the database name.
-Schemas <String[]>
The schemas of tables to generate entity types for.
-Tables <String[]>
The tables to generate entity types for.
-DataAnnotations [<SwitchParameter>]
Use attributes to configure the model (where possible). If omitted, only the fluent API is used.
-UseDatabaseNames [<SwitchParameter>]
Use table and column names directly from the database.
-Force [<SwitchParameter>]
Overwrite existing files.
-NoOnConfiguring [<SwitchParameter>]
Don't generate DbContext.OnConfiguring.
-Project <String>
The project to use.
-StartupProject <String>
The startup project to use. Defaults to the solution's startup project.
-Namespace <String>
The namespace to use. Matches the directory by default.
-ContextNamespace <String>
The namespace of the DbContext class. Matches the directory by default.
-NoPluralize [<SwitchParameter>]
Don't use the pluralizer.
-Args <String>
Arguments passed to the application.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
REMARKS
To see the examples, type: "get-help Scaffold-DbContext -examples".
For more information, type: "get-help Scaffold-DbContext -detailed".
For technical information, type: "get-help Scaffold-DbContext -full".
For online help, type: "get-help Scaffold-DbContext -online"
PM>
+++++++++++++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment