Scaffold-DbContext Stored Procedures only in EF Core

Ashkan Mobayen Khiabani

I have always used code-first with EF Core, Now I need to use Database-First. There are lots of questions, documents, and tutorials about this, teaching how to scaffold a database,

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models

However, my only requirement here is missing in all of them. I need to scaffold only a few stored procedures and views, but all these documents and question say is about how to include tables.

I was going to scaffold including everything and then delete unwanted ones manually, but it doesn't seem to be the right choice.

StepUp

It is possible to call a raw SQL using ExecuteSqlCommand. So code to call stored procedure would be look like that:

context.Database.ExecuteSqlCommand("YourStoredProcedure @p0, @p1", 
    parameters: new[] { "Joseph", "Gates" });

UPDATE:

As msdn says about how to get rows from stored procedures:

Raw SQL queries can be used to execute a stored procedure.

var user = "johndoe";

var blogs = context.Blogs
    .FromSqlRaw("EXECUTE dbo.GetMostPopularBlogsForUser {0}", user)
    .ToList();

The following example uses a raw SQL query that selects from a Table-Valued Function (TVF), then disables change tracking with the call to AsNoTracking:

var searchTerm = ".NET";

var blogs = context.Blogs
    .FromSqlInterpolated($"SELECT * FROM dbo.SearchBlogs({searchTerm})")
    .AsNoTracking()
    .ToList();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Stored Procedures in EF Core 3.0

Stored procedures EF and NET CORE

EF Core Scaffold-DbContext table not included in the selection set

Scaffold-DbContext for database views in EF Core 2.1 (Query Types)

EF Core 2.0 scaffold-dbcontext Find ConnectionString in another project

How to add stored procedures to Entity Framework Core DbContext?

EF 6 Core - Many-to-Many Relationship Database First (EF Core Power Tools/scaffold-dbcontext)

EF Core DBContext targeting only part of the database

Difference between Scaffold-DbContext and dotnet-ef-dbcontext-scaffold

How to use stored procedures with SqlParameters in EF Core 3.0

How to pass table parameters to SQL Server stored procedures in EF Core?

C#, EF Core and Scaffold-DbContext, How to not passing null for a default valued column in the database?

EF Core2.2: Scaffold-DbContext not working with named connection string in WPF project

EF core 3.1+Pomelo Scaffold-DbContext prevent view code generation

ASP.NET and EF Core - ModelState.IsValid always returns true for models generated by Scaffold-DbContext

EF reverse engineering code first and stored procedures

EF6: where to create stored procedures?

Setting up Database First EF Project Using Scaffold-DBContext

How to scaffold EF core to existing DB?

EF core DbContext with dependency injection

Abstracting ef core 2 dbContext

EF Core 6: parallelisation in dbContext

How to instantiate a DbContext in EF Core

Mocking EF core dbcontext and dbset

Singleton service and EF Core dbContext

DbContext & DbcontextPool in ef-core

How to scaffold DbContext with plural DbSet property names in Entity Framework Core?

Run Scaffold DBContext without overwriting custom code in Entity Framework core

.NET Core Entity Framework stored procedures