Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/snickler/efcore-fluentstoredprocedure
- Owner: snickler
- License: mit
- Created: 2017-05-06T22:57:47.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-05-17T16:37:42.000Z (8 months ago)
- Last Synced: 2025-01-15T02:09:05.844Z (7 days ago)
- Topics: csharp, dotnet-core, ef-core, efcore, fluent, netstandard
- Language: C#
- Homepage:
- Size: 54.7 KB
- Stars: 177
- Watchers: 20
- Forks: 34
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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;
```