Need help with StoredProcedureEFCore?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

verdie-g
164 Stars 40 Forks MIT License 235 Commits 3 Opened issues

Description

Entity Framework Core extension to execute stored procedures

Services available

!
?

Need anything else?

Contributors list

# 167,953
React
D
Nim
xml-sch...
158 commits
# 580,279
C#
swagger
swagger...
entity-...
3 commits
# 219,102
Xamarin
netstan...
inversi...
Git
1 commit
# 22,966
C#
Sass
ffmpeg
jQuery
1 commit
# 411,550
actor-m...
pony-la...
C
Shell
1 commit
# 442,121
C#
Shell
bitstam...
bittrex...
1 commit

# 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

Example

List rows = null;

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

long limitOutValue = limitOut.Value;

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

bool b = retParam.Value;

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

API

DbContext

IStoredProcBuilder LoadStoredProc(string name)

IStoredProcBuilder

IStoredProcBuilder AddParam(string name, T val); // Input parameter
IStoredProcBuilder AddParam(string name, T val, out IOutParam outParam, int size, byte precision, byte scale); // Input/Ouput parameter
IStoredProcBuilder AddParam(string name, out IOutParam outParam, int size, byte precision, byte scale); // Ouput parameter
IStoredProcBuilder ReturnValue(out IOutParam retParam, int size, byte precision, byte scale);
IStoredProcBuilder SetTimeout(int timeout);
Task               ExecAsync(Func action, CancellationToken cancellationToken);
Task          ExecNonQueryAsync(CancellationToken cancellationToken);
Task               ExecScalarAsync(Action action, CancellationToken cancellationToken);

DbDataReader

Task>                        ToListAsync();
Task>       ToDictionaryAsync(Func keyProjection);
Task>> ToLookupAsync(Func keyProjection);
Task>                     ToSetAsync();
Task>                        ColumnAsync();
Task>                        ColumnAsync(string columnName);
Task                              FirstAsync();
Task                              FirstOrDefaultAsync();
Task                              SingleAsync();
Task                              SingleOrDefaultAsync();

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

Installation

Install-Package StoredProcedureEFCore

Why ?

Stored procedure execution was previously not supported in Entity Framework Core: - Raw store access APIs: Support for ad hoc mapping of arbitrary types #1862 - Stored procedure mapping support #245

It is now supported since EF Core 2.1 but this library 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. Output parameters and return values seem difficult to use with EF Core - Mapping is 30% faster

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.