How to call Stored Procedures in ASP.NET Core
In this week's newsletter, let's talk about how we can call Stored Procedures from our ASP.NET Core API using Entity Framework Core with an illustrating example.
In this week's newsletter, let's talk about how we can call Stored Procedures from our ASP.NET Core API using Entity Framework Core with an illustrating example.
Why do you need Stored Procedures?
Stored Procedures encapsulate complex queries or programming logic which, when executed on a database produces data results. These programming structures help developers in reusing the scripts whenever required, while creating opportunities for separating database programming from the upper tiers.
3 Ways to call Stored Procedures in ASP.NET Core
With ASP.NET Core, you can call Stored Procedures in 3 ways, based on how your database interaction is designed.
If you’re using Entity Framework Core -
If your application interacts with the database using Entity Framework Core and the Stored Procedures return results from a single Entity then you call call FromSql() or FromSqlRaw() methods on the DatabaseContext.
However, If your Stored Procedures return results as DTO objects instead of a single Entity then you can’t use Entity Framework Core directly and have to go with the old school ADO.NET framework via the Database Context connection object.
Or you have the Dapper ORM -
Alternatively, you can use Dapper, which is a lightweight ORM that doesn’t have the scaffold or the mapping features of Entity Framework Core but still works with DTO objects.
You can find the complete article, where I have explained each of these in detail with illustrating examples.
You can check out the article here - https://referbruv.com/blog/working-with-stored-procedures-in-aspnet-core-ef-core/
ANNOUNCEMENT - Youtube Channel is now Live 🥳
I always wanted to explore new ways to create informative content on the technologies I had learnt and explored. I’m happy to announce that I’ve started a new Youtube Channel - https://www.youtube.com/@referbruv
I’d love for you to check out the channel and please please subscribe! ❣️❣️