Skip to content

Commit e899197

Browse files
authored
Merge pull request snickler#31 from snickler/EFCore3
Added support for EF Core 3
2 parents 7d54b3b + 9f1a50e commit e899197

File tree

2 files changed

+137
-133
lines changed

2 files changed

+137
-133
lines changed

EFCoreFluent/src/EFCoreFluent/EFExtensions.cs

Lines changed: 110 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
using Microsoft.EntityFrameworkCore;
1+
#if NETSTANDARD2_1
2+
using Microsoft.Data.SqlClient;
3+
#else
4+
using System.Data.SqlClient;
5+
#endif
6+
using Microsoft.EntityFrameworkCore;
27
using System;
38
using System.Collections.Generic;
49
using System.Data.Common;
5-
using System.Data.SqlClient;
610
using System.Linq;
711
using System.Reflection;
812
using System.Threading;
@@ -12,34 +16,39 @@ namespace Snickler.EFCore
1216
{
1317
public static class EFExtensions
1418
{
15-
/// <summary>
16-
/// Creates an initial DbCommand object based on a stored procedure name
17-
/// </summary>
18-
/// <param name="context">target database context</param>
19-
/// <param name="storedProcName">target procedure name</param>
20-
/// <param name="prependDefaultSchema">Prepend the default schema name to <paramref name="storedProcName"/> if explicitly defined in <paramref name="context"/></param>
21-
/// <param name="commandTimeout">Command timeout in seconds. Default is 30.</param>
22-
/// <returns></returns>
23-
public static DbCommand LoadStoredProc(this DbContext context, string storedProcName, bool prependDefaultSchema = true, short commandTimeout = 30)
24-
{
25-
var cmd = context.Database.GetDbConnection().CreateCommand();
26-
27-
cmd.CommandTimeout = commandTimeout;
28-
29-
if (prependDefaultSchema)
30-
{
31-
var schemaName = context.Model.Relational().DefaultSchema;
32-
if (schemaName != null)
33-
{
34-
storedProcName = $"{schemaName}.{storedProcName}";
35-
}
36-
}
37-
38-
cmd.CommandText = storedProcName;
39-
cmd.CommandType = System.Data.CommandType.StoredProcedure;
40-
41-
return cmd;
42-
}
19+
/// <summary>
20+
/// Creates an initial DbCommand object based on a stored procedure name
21+
/// </summary>
22+
/// <param name="context">target database context</param>
23+
/// <param name="storedProcName">target procedure name</param>
24+
/// <param name="prependDefaultSchema">Prepend the default schema name to <paramref name="storedProcName"/> if explicitly defined in <paramref name="context"/></param>
25+
/// <param name="commandTimeout">Command timeout in seconds. Default is 30.</param>
26+
/// <returns></returns>
27+
public static DbCommand LoadStoredProc(this DbContext context, string storedProcName, bool prependDefaultSchema = true, short commandTimeout = 30)
28+
{
29+
30+
var cmd = context.Database.GetDbConnection().CreateCommand();
31+
32+
cmd.CommandTimeout = commandTimeout;
33+
34+
if (prependDefaultSchema)
35+
{
36+
#if NETSTANDARD2_1
37+
var schemaName = context.Model.GetDefaultSchema();
38+
#else
39+
var schemaName = context.Model.Relational().DefaultSchema;
40+
#endif
41+
if (schemaName != null)
42+
{
43+
storedProcName = $"{schemaName}.{storedProcName}";
44+
}
45+
}
46+
47+
cmd.CommandText = storedProcName;
48+
cmd.CommandType = System.Data.CommandType.StoredProcedure;
49+
50+
return cmd;
51+
}
4352

4453
/// <summary>
4554
/// Creates a DbParameter object and adds it to a DbCommand
@@ -93,9 +102,6 @@ public static DbCommand WithSqlParam(this DbCommand cmd, string paramName, SqlPa
93102
if (string.IsNullOrEmpty(cmd.CommandText) && cmd.CommandType != System.Data.CommandType.StoredProcedure)
94103
throw new InvalidOperationException("Call LoadStoredProc before using this method");
95104

96-
//var param = cmd.CreateParameter();
97-
//param.ParameterName = paramName;
98-
//configureParam?.Invoke(param);
99105
cmd.Parameters.Add(parameter);
100106

101107
return cmd;
@@ -104,12 +110,10 @@ public static DbCommand WithSqlParam(this DbCommand cmd, string paramName, SqlPa
104110
public class SprocResults
105111
{
106112

107-
// private DbCommand _command;
108113
private DbDataReader _reader;
109114

110115
public SprocResults(DbDataReader reader)
111116
{
112-
// _command = command;
113117
_reader = reader;
114118
}
115119

@@ -216,13 +220,12 @@ public static void ExecuteStoredProc(this DbCommand command, Action<SprocResults
216220
using (var reader = command.ExecuteReader(commandBehaviour))
217221
{
218222
var sprocResults = new SprocResults(reader);
219-
// return new SprocResults();
220223
handleResults(sprocResults);
221224
}
222225
}
223226
finally
224227
{
225-
if(manageConnection)
228+
if (manageConnection)
226229
{
227230
command.Connection.Close();
228231
}
@@ -264,75 +267,75 @@ public static void ExecuteStoredProc(this DbCommand command, Action<SprocResults
264267
}
265268
}
266269
}
267-
268-
/// <summary>
269-
/// Executes a non-query.
270-
/// </summary>
271-
/// <param name="command"></param>
272-
/// <param name="commandBehaviour"></param>
273-
/// <param name="manageConnection"></param>
274-
/// <returns></returns>
275-
public static int ExecuteStoredNonQuery(this DbCommand command, System.Data.CommandBehavior commandBehaviour = System.Data.CommandBehavior.Default, bool manageConnection = true)
276-
{
277-
int numberOfRecordsAffected = -1;
278-
279-
using (command)
280-
{
281-
if (command.Connection.State == System.Data.ConnectionState.Closed)
282-
{
283-
command.Connection.Open();
284-
}
285-
286-
try
287-
{
288-
numberOfRecordsAffected = command.ExecuteNonQuery();
289-
}
290-
finally
291-
{
292-
if (manageConnection)
293-
{
294-
command.Connection.Close();
295-
}
296-
}
297-
}
298-
299-
return numberOfRecordsAffected;
300-
}
301-
302-
/// <summary>
303-
/// Executes a non-query asynchronously.
304-
/// </summary>
305-
/// <param name="command"></param>
306-
/// <param name="commandBehaviour"></param>
307-
/// <param name="ct"></param>
308-
/// <param name="manageConnection"></param>
309-
/// <returns></returns>
310-
public async static Task<int> ExecuteStoredNonQueryAsync(this DbCommand command, System.Data.CommandBehavior commandBehaviour = System.Data.CommandBehavior.Default, CancellationToken ct = default(CancellationToken), bool manageConnection = true)
311-
{
312-
int numberOfRecordsAffected = -1;
313-
314-
using (command)
315-
{
316-
if (command.Connection.State == System.Data.ConnectionState.Closed)
317-
{
318-
await command.Connection.OpenAsync(ct).ConfigureAwait(false);
319-
}
320-
321-
try
322-
{
323-
numberOfRecordsAffected = await command.ExecuteNonQueryAsync().ConfigureAwait(false);
324-
}
325-
finally
326-
{
327-
if (manageConnection)
328-
{
329-
command.Connection.Close();
330-
}
331-
}
332-
}
333-
334-
return numberOfRecordsAffected;
335-
}
336-
270+
271+
/// <summary>
272+
/// Executes a non-query.
273+
/// </summary>
274+
/// <param name="command"></param>
275+
/// <param name="commandBehaviour"></param>
276+
/// <param name="manageConnection"></param>
277+
/// <returns></returns>
278+
public static int ExecuteStoredNonQuery(this DbCommand command, System.Data.CommandBehavior commandBehaviour = System.Data.CommandBehavior.Default, bool manageConnection = true)
279+
{
280+
int numberOfRecordsAffected = -1;
281+
282+
using (command)
283+
{
284+
if (command.Connection.State == System.Data.ConnectionState.Closed)
285+
{
286+
command.Connection.Open();
287+
}
288+
289+
try
290+
{
291+
numberOfRecordsAffected = command.ExecuteNonQuery();
292+
}
293+
finally
294+
{
295+
if (manageConnection)
296+
{
297+
command.Connection.Close();
298+
}
299+
}
300+
}
301+
302+
return numberOfRecordsAffected;
303+
}
304+
305+
/// <summary>
306+
/// Executes a non-query asynchronously.
307+
/// </summary>
308+
/// <param name="command"></param>
309+
/// <param name="commandBehaviour"></param>
310+
/// <param name="ct"></param>
311+
/// <param name="manageConnection"></param>
312+
/// <returns></returns>
313+
public async static Task<int> ExecuteStoredNonQueryAsync(this DbCommand command, System.Data.CommandBehavior commandBehaviour = System.Data.CommandBehavior.Default, CancellationToken ct = default(CancellationToken), bool manageConnection = true)
314+
{
315+
int numberOfRecordsAffected = -1;
316+
317+
using (command)
318+
{
319+
if (command.Connection.State == System.Data.ConnectionState.Closed)
320+
{
321+
await command.Connection.OpenAsync(ct).ConfigureAwait(false);
322+
}
323+
324+
try
325+
{
326+
numberOfRecordsAffected = await command.ExecuteNonQueryAsync().ConfigureAwait(false);
327+
}
328+
finally
329+
{
330+
if (manageConnection)
331+
{
332+
command.Connection.Close();
333+
}
334+
}
335+
}
336+
337+
return numberOfRecordsAffected;
338+
}
339+
337340
}
338341
}
Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<TargetFrameworks>netstandard1.4;netstandard2.0</TargetFrameworks>
5-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<AssemblyVersion>1.0.2</AssemblyVersion>
7-
<FileVersion>1.0.2</FileVersion>
8-
<PackageTags>snickler.efcore,entityframeworkcore,fluent,storedprocedure</PackageTags>
9-
<PackageProjectUrl>https://github.com/snickler/EFCore-FluentStoredProcedure</PackageProjectUrl>
10-
<Copyright></Copyright>
11-
<PackageLicenseUrl>https://github.com/snickler/EFCore-FluentStoredProcedure/blob/master/LICENSE</PackageLicenseUrl>
12-
<Authors>Jeremy Sinclair, contributors</Authors>
13-
<Version>1.0.2</Version>
14-
<Description>Fluent Stored Procedure Extensions for EntityFrameworkCore</Description>
15-
<PackageId>Snickler.EFCore</PackageId>
16-
<Product>Snickler.EFCore</Product>
17-
</PropertyGroup>
18-
19-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.4' ">
20-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.5" />
21-
</ItemGroup>
22-
23-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
24-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.*" />
25-
</ItemGroup>
26-
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard1.4;netstandard2.0;netstandard2.1</TargetFrameworks>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
<AssemblyVersion>1.1</AssemblyVersion>
7+
<FileVersion>1.1</FileVersion>
8+
<PackageTags>snickler.efcore,entityframeworkcore,fluent,storedprocedure</PackageTags>
9+
<PackageProjectUrl>https://github.com/snickler/EFCore-FluentStoredProcedure</PackageProjectUrl>
10+
<Copyright>
11+
</Copyright>
12+
<PackageLicenseUrl>https://github.com/snickler/EFCore-FluentStoredProcedure/blob/master/LICENSE</PackageLicenseUrl>
13+
<Authors>Jeremy Sinclair, contributors</Authors>
14+
<Version>1.1</Version>
15+
<Description>Fluent Stored Procedure Extensions for EntityFrameworkCore</Description>
16+
<PackageId>Snickler.EFCore</PackageId>
17+
<Product>Snickler.EFCore</Product>
18+
</PropertyGroup>
19+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.4' ">
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.6" />
21+
</ItemGroup>
22+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
23+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.*" />
24+
</ItemGroup>
25+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
26+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.*" />
27+
</ItemGroup>
2728
</Project>

0 commit comments

Comments
 (0)