StoredProcedureEFCore 0.3.11

Entity Framework Core extension to execute a stored procedure and map the result

Install-Package StoredProcedureEFCore -Version 0.3.11
dotnet add package StoredProcedureEFCore --version 0.3.11
<PackageReference Include="StoredProcedureEFCore" Version="0.3.11" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StoredProcedureEFCore --version 0.3.11
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Execute stored procedures with Entity Framework Core

DbContext extension with LoadStoredProc method which creates
an IStoredProcBuilder to build a stored procedure with a custom
mapping strategy using the provided DbDataReader extension.

The method handles :

  • Extra column in result set
  • Extra property in model
  • Null values in result set
  • Underscores in result set column names ("column_name" is mapped to ColumnName property)
  • Int (db) to enumeration (result model) mapping

Entity Framework Core supports stored procedure since 2.1

This library will soon be obsolete, but it has few advantages compared to FromSql:

  • Extra property in the model won't throw an exception. The property keeps its default value
  • The interface is easier to use (I think). Output parameters and return values seem difficult
    to use with EFCore
  • It is 30% faster

Example

List<Model> rows = null;

ctx.LoadStoredProc("dbo.ListAll")
   .AddParam("limit", 300L)
   .AddParam("limitOut", out IOutParam<long> limitOut)
   .Exec(r => rows = r.ToList<Model>());

long limitOutValue = limitOut.Value;

ctx.LoadStoredProc("dbo.ReturnBoolean")
   .AddParam("boolean_to_return", true)
   .ReturnValue(out IOutParam<bool> retParam)
   .ExecNonQuery();

bool b = retParam.Value;

ctx.LoadStoredProc("dbo.ListAll")
   .AddParam("limit", 1L)
   .ExecScalar(out long l);

API

DbContext

IStoredProcBuilder             LoadStoredProc(string name)

DbDataReader

List<T>                              ToList<T>()
Dictionary<TKey, TValue>             ToDictionary<TKey, TValue>(Func<TValue, TKey> keyProjection)
Dictionary<TKey, List<TValue>>       ToLookup<TKey, TValue>(Func<TValue, TKey> keyProjection)
HashSet<T>                           ToSet<T>()
List<T>                              Column<T>()
List<T>                              Column<T>(string columnName)
T                                    First<T>()
T                                    FirstOrDefault<T>()
T                                    Single<T>()
T                                    SingleOrDefault<T>()

All these methods have a corresponding async method : ToListAsync, ToDictionaryAsync, ...

IStoredProcBuilder

IStoredProcBuilder                   AddParam<T>(string name, T val)                             // Input parameter
IStoredProcBuilder                   AddParam<T>(string name, T val, out IOutParam<T> outParam)  // Input/Ouput parameter
IStoredProcBuilder                   AddParam<T>(string name, out IOutParam<T> outParam)         // Ouput parameter
IStoredProcBuilder                   ReturnValue<T>(out IOutParam<T> retParam)
void                                 Exec(Action<DbDataReader> action)
void                                 ExecNonQuery()
void                                 ExecScalar<T>(out T val)

Exec, ExecNonQuery, and ExecScalar have a corresponding async method.

Installation

Install-Package StoredProcedureEFCore

Why ?

Stored procedure execution was not supported in entity framework core:

Execute stored procedures with Entity Framework Core

DbContext extension with LoadStoredProc method which creates
an IStoredProcBuilder to build a stored procedure with a custom
mapping strategy using the provided DbDataReader extension.

The method handles :

  • Extra column in result set
  • Extra property in model
  • Null values in result set
  • Underscores in result set column names ("column_name" is mapped to ColumnName property)
  • Int (db) to enumeration (result model) mapping

Entity Framework Core supports stored procedure since 2.1

This library will soon be obsolete, but it has few advantages compared to FromSql:

  • Extra property in the model won't throw an exception. The property keeps its default value
  • The interface is easier to use (I think). Output parameters and return values seem difficult
    to use with EFCore
  • It is 30% faster

Example

List<Model> rows = null;

ctx.LoadStoredProc("dbo.ListAll")
   .AddParam("limit", 300L)
   .AddParam("limitOut", out IOutParam<long> limitOut)
   .Exec(r => rows = r.ToList<Model>());

long limitOutValue = limitOut.Value;

ctx.LoadStoredProc("dbo.ReturnBoolean")
   .AddParam("boolean_to_return", true)
   .ReturnValue(out IOutParam<bool> retParam)
   .ExecNonQuery();

bool b = retParam.Value;

ctx.LoadStoredProc("dbo.ListAll")
   .AddParam("limit", 1L)
   .ExecScalar(out long l);

API

DbContext

IStoredProcBuilder             LoadStoredProc(string name)

DbDataReader

List<T>                              ToList<T>()
Dictionary<TKey, TValue>             ToDictionary<TKey, TValue>(Func<TValue, TKey> keyProjection)
Dictionary<TKey, List<TValue>>       ToLookup<TKey, TValue>(Func<TValue, TKey> keyProjection)
HashSet<T>                           ToSet<T>()
List<T>                              Column<T>()
List<T>                              Column<T>(string columnName)
T                                    First<T>()
T                                    FirstOrDefault<T>()
T                                    Single<T>()
T                                    SingleOrDefault<T>()

All these methods have a corresponding async method : ToListAsync, ToDictionaryAsync, ...

IStoredProcBuilder

IStoredProcBuilder                   AddParam<T>(string name, T val)                             // Input parameter
IStoredProcBuilder                   AddParam<T>(string name, T val, out IOutParam<T> outParam)  // Input/Ouput parameter
IStoredProcBuilder                   AddParam<T>(string name, out IOutParam<T> outParam)         // Ouput parameter
IStoredProcBuilder                   ReturnValue<T>(out IOutParam<T> retParam)
void                                 Exec(Action<DbDataReader> action)
void                                 ExecNonQuery()
void                                 ExecScalar<T>(out T val)

Exec, ExecNonQuery, and ExecScalar have a corresponding async method.

Installation

Install-Package StoredProcedureEFCore

Why ?

Stored procedure execution was not supported in entity framework core:

Version History

Version Downloads Last updated
0.3.11 166 6/1/2019
0.3.10 4,322 11/15/2018
0.3.9 5,686 5/4/2018
0.3.8 641 4/21/2018
0.3.7 501 4/3/2018
0.3.6 482 3/16/2018
0.3.5 196 3/16/2018
0.3.4 556 3/2/2018
0.3.3 586 2/1/2018
0.3.2 339 1/19/2018
0.3.1 315 1/8/2018
0.3.0 276 1/8/2018
0.2.0 325 12/29/2017
0.1.1 301 12/28/2017
0.1.0 251 12/26/2017