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 the 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 for the 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 procedures.
  • Click Here for more details: https://blogs.msdn.microsoft.com/webdev/2014/01/14/scaffolding-ado-net-entity-data-model-designer-based-models/

Now, Let’s Discuss Entity Framework Core

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

  • SDK Version 1. x is starting from 1.0.0 previewer to 1.2.2 previewer whereas for 2.0 it is 2.0.0.
  • EF Core 1.x manages 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.

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 the connection provider you need to write your provider like ‘SQL Server’,’ Oracle’ and OutputDir is used to store these entities in 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)

 

Also Read: What is Full-Text Search? Implementing SQL Server Full-Text Search In An ASP.NET MVC Websites With Entity Framework

Process For Stored Procedure Scaffolding:

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


modelBuilder.Entity(entity =>
	{
		entity.Property(t => t.PropertyName)
				.HasColumnName("Column_Name_In_Database");
	});

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


public IEnumerable GetAllExercicesAudit(DateTime dateStart, DateTime dateEnd, Guid IdAgent, Guid IdProf)
	{
		var dateStartParam = new SqlParameter("@dateStart", dateStart);
		var dateEndParam = new SqlParameter("@dateEnd", dateEnd);
		var IdAgentParam = new SqlParameter("@IdAgent", IdAgent);
		var IdProfParam = new SqlParameter("@IdProf", IdProf);
	
		return Set()
			.FromSql("GetAllExercicesAudit @dateStart, @dateEnd, @IdAgent, @IdProf", 
						parameters: new[] { dateStartParam, dateEndParam, IdAgentParam, IdProfParam });
	}

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

2. The second without-parameter method is like this


public IEnumerable GetExercicesAgents()
	{
		return Set()
			.FromSql("GetExercicesAgents");
	}

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

You need to write this type of method in a context file so you can directly get these methods 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 this topic?

  • https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
  • https://ef.readthedocs.io/en/staging/miscellaneous/cli/dotnet.html
  • https://www.talkingdotnet.com/how-to-execute-stored-procedure-in-entity-framework-core
Article by

Chintan Prajapati

Chintan Prajapati, a seasoned computer engineer with over 20 years in the software industry, is the Founder and CEO of Satva Solutions. His expertise lies in Accounting & ERP Integrations, RPA, and developing technology solutions around leading ERP and accounting software, focusing on using Responsible AI and ML in fintech solutions. Chintan holds a BE in Computer Engineering and is a Microsoft Certified Professional, Microsoft Certified Technology Specialist, Certified Azure Solution Developer, Certified Intuit Developer, and Xero Developer.Throughout his career, Chintan has significantly impacted the accounting industry by consulting and delivering integrations and automation solutions that have saved thousands of man-hours. He aims to provide readers with insightful, practical advice on leveraging technology for business efficiency.Outside of his professional work, Chintan enjoys trekking and bird-watching. Guided by the philosophy, "Deliver the highest value to clients". Chintan continues to drive innovation and excellence in digital transformation strategies from his base in Ahmedabad, India.