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 few questions.

What is Scaffolding?

Scaffolding is a great 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 it’s 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

                 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 Version only Required Some Packages more in 1.x Versions.

Steps for Scaffolding

Nuget Installation:

  • First You need to Install 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’.

If it is executed then Type Command ‘ScaffoldDbContext Connection String ConnectionProvider OutputDir Models Where in place of Connection String you need to Write your Connection String at Place of Connection Provider you need to write your Provider like ‘Sql Server’,’Oracle’ and OutputDir is use for Store this Entities to That Folder(NOTE: You can only use that directory which is available in that project you can’t give another place path).

For Example of this ScaffoldDbContext “Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServerOutputDir Models

At the Execution of this Command, You Will Get All Entities Of Database and Context file.

Please check Following Link For more about scaffold Window.

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 Option You can Create your own execution Method Also you need to Create Model Builder for it.  First You Need to create Entities of that Stored Procedures Create Model Builder Like this.

 modelBuilder.Entity<stored procedure EntityName>(entity => {
		  entity.Property(t => t.Properties);
     });

Then After you need to Write 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_Result> 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<GetAllExercicesAudit_Result>().FromSql("GetAllExercicesAudit      @dateStart,@dateEnd,@IdAgent,@IdProf", parameters: new[] { dateStartParam,      dateEndParam, IdAgentParam, IdProfParam });}

In Above Method ‘GetAllExercicesAudit_Result’ is Stored Procedure Entity and GetAllExercicesAuditis Stored Procedure Name With Parameter.

2. Second without Parameter Method is like this

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

There is an only Need Entity and Stored Procedure Name.

For All Other Stored Procedure, There is a One Common Execution Method.

public virtual int RemoveExerciceFromFavoris(string idUser, int idExercice)
{    var idUserParam = new SqlParameter("@idUser", idUser);
     var idExerciceParam = new SqlParameter("@idExercice", idExercice);
    return Database.ExecuteSqlCommand("RemoveExerciceFromFavoris   @idUser,@idExercice",    parameters: new[] { idUserParam, idExerciceParam });      }

Example there is an ‘ExecuteSqlCommand’ To Execute Procedure it will return only Execution Success or Failed 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 a option for scaffolding of stored procedure so scaffold entities of stored procedure from there and use it in EF Core.

By | 2018-01-04T10:50:57+00:00 December 30th, 2017|Asp.Net, ASP.NET Core|