Hello,
Question 1: I wanted to know how we can scaffold existing stored procedure in Entity Framework Core 6 using DB First approch? If yes Please share me the steps.
Question 2: Is there a way to Scaffold only Specific tables and Stored Procedures in EF Core 6?

Jignesh KumarRank #91 · 22.9K reputationPosted Dec 17, 2023, 1:46 AM EST
Hello Manikandan,
Answer to your first question : Yes,
Please follow below steps,
Step-1: Install Entity Framework Core Tools:
dotnet tool install --global dotnet-efStep-2: Install EF Core Design Package:
dotnet add package Microsoft.EntityFrameworkCore.DesignStep-3: Create a Database Context: If you have existing then verify with this one
public class YourDbContext : DbContext { public YourDbContext(DbContextOptions options) : base(options) { } // DbSet properties for your entities }Step-4: Scaffold the Stored Procedure:
dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer -o Models -t YourStoredProcedureNameStep-5: Use the Stored Procedure in Code:
var result = dbContext.YourStoredProcedureName(parameter1, parameter2);Hope this will clear your both questions.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 17, 2023, 11:27 AM EST
Hello,
Yes, it is possible to scaffold existing stored procedures in Entity Framework Core 6 using the Database First approach. Here are the steps to scaffold a stored procedure:
dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer --context-dir Context --context YourDbContext --no-onconfiguring --no-pluralize --no-views --no-functions --no-stored-procedures --stored-proceduresReplace "YourConnectionString" with the connection string to your database and "YourDbContext" with the name of your DbContext class.
This command will scaffold all the stored procedures in your database and generate the necessary code in the specified DbContext class.
To scaffold only specific tables and stored procedures, you can modify the command as follows:
dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer --context-dir Context --context YourDbContext --no-onconfiguring --no-pluralize --no-views --no-functions --no-stored-procedures --tables Table1 Table2 --stored-procedures StoredProcedure1 StoredProcedure2Replace "Table1", "Table2", "StoredProcedure1", and "StoredProcedure2" with the names of the specific tables and stored procedures you want to scaffold.
By specifying the tables and stored procedures in the command, only those entities will be scaffolded in the generated code.