In my previous post I showed how you can map and use stored procedures manually from EF Core, a process which involved quite a bit of code, and some changes to your derived DbContext.

With the latest release of EF Core Power Tools, you can opt-in to have any SQL Server stored procedures in your database made available to you.

To include stored procedures, select "Generate stored procedure mappings (preview)" from the options dialog.

If you do that, all the required code to define result POCO classes, and calling your stored procedures will be automagically generated for you, and you will be able to call your procedure with a single line of code!

So for example you can write code like this to get results back from a stored procedure in the Northwind database:

using (var db = new NorthwindContext())
{
    var procedures = new NorthwindContextProcedures(db);
    var result = await procedures.CustOrderHist("ALFKI");

    foreach (var item in result)
        Console.WriteLine($"{item.ProductName}: {item.Total}");
}

If you have output parameters, slightly more code is required (to create a variable to hold the result from each output variable):

var outOverallCount = new OutputParameter<int>();
var customers = await procedures.SP_GET_TOP_IDS(10, outOverallCount);
Console.WriteLine($"Db contains {outOverallCount.Value} Customers.");
foreach (var customer in customers)
    Console.WriteLine(customer.CustomerId);

The result classes are not added to your DbContext, instead a "temporary" DbContext is created for each query, as you can see in the DbContextExtensions class, that is part of the generated code.

Please try out the feature, and let me know any feedback you may have via the GitHub issue tracker.

Comments or questions for this blog post?