Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/snickler/efcore-fluentstoredprocedure

EFCore Extension that allows a means to map a stored procedure to a class, fluently.
https://github.com/snickler/efcore-fluentstoredprocedure

csharp dotnet-core ef-core efcore fluent netstandard

Last synced: about 11 hours ago
JSON representation

EFCore Extension that allows a means to map a stored procedure to a class, fluently.

Awesome Lists containing this project

README

        

# Snickler.EFCore
Fluent Methods for mapping Stored Procedure results to objects in EntityFrameworkCore

[![NuGet](https://img.shields.io/nuget/v/Snickler.EFCore.svg)](https://www.nuget.org/packages/Snickler.EFCore)

## Usage

### Executing A Stored Procedure

Add the `using` statement to pull in the extension method. E.g: `using Snickler.EFCore`

```csharp

var dbContext = GetDbContext();
dbContext.LoadStoredProc("dbo.SomeSproc")
.WithSqlParam("fooId", 1)
.ExecuteStoredProc((handler) =>
{
var fooResults = handler.ReadToList();
// do something with your results.
});

```

### Handling Multiple Result Sets

```csharp

var dbContext = GetDbContext();
dbContext.LoadStoredProc("dbo.SomeSproc")
.WithSqlParam("fooId", 1)
.ExecuteStoredProc((handler) =>
{
var fooResults = handler.ReadToList();
handler.NextResult();
var barResults = handler.ReadToList();
handler.NextResult();
var bazResults = handler.ReadToList()
});

```

### Handling Output Parameters

```csharp

DbParameter outputParam = null;

var dbContext = GetDbContext();
dbContext.LoadStoredProc("dbo.SomeSproc")
.WithSqlParam("fooId", 1)
.WithSqlParam("myOutputParam", (dbParam) =>
{
dbParam.Direction = System.Data.ParameterDirection.Output;
dbParam.DbType = System.Data.DbType.Int32;
outputParam = dbParam;
})
.ExecuteStoredProc((handler) =>
{
var fooResults = handler.ReadToList();
handler.NextResult();
var barResults = handler.ReadToList();
handler.NextResult();
var bazResults = handler.ReadToList()
});

int outputParamValue = (int)outputParam?.Value;

```

### Using output parameters without returning a result set

```csharp

DbParameter outputParam = null;

var dbContext = GetDbContext();

await dbContext.LoadStoredProc("dbo.SomeSproc")
.WithSqlParam("InputParam1", 1)
.WithSqlParam("myOutputParam", (dbParam) =>
{
dbParam.Direction = System.Data.ParameterDirection.Output;
dbParam.DbType = System.Data.DbType.Int16;
outputParam = dbParam;
})

.ExecuteStoredNonQueryAsync();

int outputParamValue = (short)outputParam.Value;

```

### Using output parameters without returning a result set but also getting the number of rows affected

Make sure your stored procedure does not contain `SET NOCOUNT ON`.

```csharp
int numberOfRowsAffected = -1;

DbParameter outputParam = null;

var dbContext = GetDbContext();

numberOfRowsAffected = await dbContext.LoadStoredProc("dbo.SomeSproc")
.WithSqlParam("InputParam1", 1)
.WithSqlParam("myOutputParam", (dbParam) =>
{
dbParam.Direction = System.Data.ParameterDirection.Output;
dbParam.DbType = System.Data.DbType.Int16;
outputParam = dbParam;
})

.ExecuteStoredNonQueryAsync();

int outputParamValue = (short)outputParam.Value;

```

### Changing the execution timeout when waiting for a stored procedure to return

```csharp

DbParameter outputParam = null;

var dbContext = GetDbContext();

// change timeout from 30 seconds to 300 seconds (5 minutes)
await dbContext.LoadStoredProc("dbo.SomeSproc", commandTimeout:300)
.WithSqlParam("InputParam1", 1)
.WithSqlParam("myOutputParam", (dbParam) =>
{
dbParam.Direction = System.Data.ParameterDirection.Output;
dbParam.DbType = System.Data.DbType.Int16;
outputParam = dbParam;
})

.ExecuteStoredNonQueryAsync();

int outputParamValue = (short)outputParam.Value;

```