Code Monkey home page Code Monkey logo

storedprocedureefcore's Introduction

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<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)

IStoredProcBuilder

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

DbDataReader

Task<List<T>>                        ToListAsync<T>();
Task<Dictionary<TKey, TValue>>       ToDictionaryAsync<TKey, TValue>(Func<TValue, TKey> keyProjection);
Task<Dictionary<TKey, List<TValue>>> ToLookupAsync<TKey, TValue>(Func<TValue, TKey> keyProjection);
Task<HashSet<T>>                     ToSetAsync<T>();
Task<List<T>>                        ColumnAsync<T>();
Task<List<T>>                        ColumnAsync<T>(string columnName);
Task<T>                              FirstAsync<T>();
Task<T>                              FirstOrDefaultAsync<T>();
Task<T>                              SingleAsync<T>();
Task<T>                              SingleOrDefaultAsync<T>();

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:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.