Generate the code files Using Visual Studio and CLI
If you are working with Database First Approach while using Entity Framework Core, then there will be a requirement to generate the code files or Model files for the given Database. To do, we have a couple of approaches and the following are those in short.
Approach-1: Using CLI
In this approach, we can generate Model files using the Dotnet CLI tool with the following command.
dotnet ef dbcontext scaffold "Server=.\;Database=AdventureWorks;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Model -c "AdventureContext"
Here, all the code files for all tables in the AdventureWorks Database will be generated in a folder named Model.
Approach-2: Using Visual Studio
In this approach, we can even generate code files using Visual Studios PackageManager Console Commands as follows.
PM> Scaffold-DbContext "Server=.\;Database=AdventureWorks;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Context "AdventureContext"
Here too, all the code files for all tables in the AdventureWorks Database will be generated in a folder named Model.
In both the approaches, if you want to generate code files only for specific tables we can provide those by using -t switch as follows.
PM> Scaffold-DbContext "Server=.\;Database=AdventureWorksLT2012;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Context "AdventureContext" -t Users
Here only Users code file will be generated.