Scaffold Entity framework core SQL Tables

Today in this article, we shall see some basic examples of how to scaffold Entity framework core SQL Tables using the database first approach where we will create scaffolding for the existing database.

This article will help you get started with the basics of Scaffolding in the Entity Framework.

For more details on commands, please visit here.

How to Install and set up EFCore Tools

Below tools required,

  • Using dotnet ef dbcontext scaffold ( Global support for all EFCore projects)

The above setup and many more commands are already explained in detail in the below article,

Scaffold Entity framework core Single Tables

Command

Using the Package Manager console, please run the below command,

PM> Scaffold-DbContext "Server=x.x.x.x.x.\SQLEXPRESS;Database=master;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Tables TheCodeBuzzDB -OutputDir Models -ContextDir Context -Context EmployeeContext

Above we have used Tables “TheCodeBuzzDB“.

The generated Context and Models folders with entity classes look as below,

Scaffold Entity framework core SQL Tables

The generated Employee context is as shown below,

Scaffold SQL

Scaffold Entity framework core Multiple Tables

We shall now Scaffold Entity framework core Multiple Tables i.e. two tables TheCodeBuzzDB, Employee.

Command

PM> Scaffold-DbContext "Server=x.x.x.x.x.\SQLEXPRESS;Database=master;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Tables TheCodeBuzzDB,Employee -OutputDir Models -ContextDir Context -Context EmployeeContext

Above we have used Tables “TheCodeBuzzDB” and “Employee

The generated Context and Models folders with entity classes look as below,

Scaffold Entity Framework Core SQL

The generated EmployeeContext is as shown below,

public partial class EmployeeContext : DbContext
     {
         public EmployeeContext()
         {
         }

     public EmployeeContext(DbContextOptions<EmployeeContext> options)         : base(options)     {   
  
}   

  
public virtual DbSet<Employee> Employee { get; set; }     

public virtual DbSet<TheCodeBuzzDb> TheCodeBuzzDb { get; set; }

Scaffold Entity framework core All Tables

Command

Using the Package Manager console, please run the below command,

PM> Scaffold-DbContext "Server=x.x.x.x.x.\SQLEXPRESS;Database=master;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -ContextDir Context -Context EmployeeContext

Above we have not specified the Table’s names, hence the command will create scaffolding for all the available tables in the given database.

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *