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 14 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 (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2024-05-17T16:37:42.000Z (12 months ago)
- Last Synced: 2024-10-31T19:42:24.956Z (6 months ago)
- Topics: csharp, dotnet-core, ef-core, efcore, fluent, netstandard
- Language: C#
- Homepage:
- Size: 54.7 KB
- Stars: 178
- Watchers: 20
- Forks: 34
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-entity-framework-core - Snickler.EFCore - EFCore Extension that allows a means to map a stored procedure to a class, fluently. (Unsupported Packages / Contents)
README
# Snickler.EFCore
Fluent Methods for mapping Stored Procedure results to objects in EntityFrameworkCore[](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;
```