How To Do Entity Framework Core Scaffolding Process In Asp.Net Core

By: Jenith Thakkar
Updated: December 30, 2017 | Technology: Asp.Net, ASP.NET Core
How-To-Do-Entity-Framework-Core-Scaffolding-Process-In-Asp.Net-Core

Hi,

Today we are discussing how to scaffolding in asp.net core 1.x and 2.0.

Before starting scaffolding, let us discuss a few questions.

What is Scaffolding in MVC Framework?

Scaffolding is an excellent way of kick-starting model-view-controller (MVC) framework development. Visual Studio’s MVC Scaffolding uses templates to generate the routine code that is common to all ASP.NET MVC builds, such as data access and Web API. It allows the developer to concentrate on what is unique to the application.

How is Scaffolding working till Entity Framework 6.0?

  • There is an option of ADO.NET Model. When we select our database class and table that time-based on that connection its scaffold all entities of tables and stored procedure.
  • Click Here for more details.

Now, Let’s Discuss Entity Framework Core

What is The Difference Between EF Core 1.x and EF Core 2.0?

  • SDK Version of 1.x is starting from 1.0.0 previewer to 1.2.2 previewer where for 2.0 it is 2.0.0.
  • EF Core 1.x manage individual NuGet packages for entity framework core.
    • e.g. Microsoft.EntityFrameworkCore.Design
    • e.g. Microsoft.EntityFrameworkCore.SqlServer

EF Core 2.0 required only one package to set up entity framework core all functionalities e.g. Microsoft.EntityFrameworkCore.All. In terms of scaffolding, there is a no big difference between these versions only required some packages more in 1.x versions.

Get In Touch

Want to Migrate from ASP.NET Core 1.x to 2.0?

We are ready to help you, Let’s have a short meeting to start working together!

Get In Touch

Steps for Scaffolding:

Nuget Installation:

  • First of all, you need to install the following NuGet packages.
  • Microsoft.EntityFrameworkCore.Tools.DotNet, If .NET Core 2.0 then Microsoft.EntityFrameworkCore.All if .Net Core 1.x then.
  • Microsoft.EntityFrameworkCore.Design, Microsoft.EntityFrameworkCore.Tools.

Scaffold Process:

  • After installing this open your main project folder and open command window here (Shift+Mouse Right click).
  • Then type command ‘dotnet ef’.
    asp.net core
    asp.net core
  • If it is executed then type command ‘Scaffold–DbContext Connection String ConnectionProvider –OutputDir Models Where in place of Connection String. You need to write your connection string at the place of connection provider you need to write your provider like ‘SQL Server’,’Oracle’ and OutputDir is used to store these entities to that folder.
  • For Example of this Scaffold–DbContext “Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer –OutputDir Models
    asp.net core
  • At the execution of this command, You will get all entities of database and context file.
  • Please check following Link For for more about scaffold window.

(NOTE: You can only use that directory which is available in that project you can’t give another place path)

Process For Stored Procedure Scaffolding:

This is used for Scaffold SQL tables to entities. In ASP.NET Core currently, there is a no option to scaffold the stored procedure, But there is an option to execute the stored procedure based on that execute the option you can create your own execution method also you need to create a model builder for it. First of all, you need to create entities of that stored procedures create model builder like this.

Copy to Clipboard

Then after you need to write an execution method based on that stored procedure entity (this is only suitable for getting stored procedure).

1. First With Parameter Method is like this

Copy to Clipboard

In the above method ‘GetAllExercicesAudit_Result’ is stored procedure entity and ‘GetAllExercicesAudit’ is stored procedure name with parameter.

2. Second without Parameter Method is like this

Copy to Clipboard

Example there is an ‘ExecuteSqlCommand’ to execute procedure it will return only execution success or failure so I use virtual int return type.

You need to write this type of methods in context file so you can directly get these methods to anywhere in the project.

Any easiest way to get entities of Stored Procedure?

In EF 6 there is an option for scaffolding of stored procedure so scaffold entities of the stored procedure from there and use it in EF Core.

Which are good references to learn about more on this topic?