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 ;
2
7
using System ;
3
8
using System . Collections . Generic ;
4
9
using System . Data . Common ;
5
- using System . Data . SqlClient ;
6
10
using System . Linq ;
7
11
using System . Reflection ;
8
12
using System . Threading ;
@@ -12,34 +16,39 @@ namespace Snickler.EFCore
12
16
{
13
17
public static class EFExtensions
14
18
{
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
+ }
43
52
44
53
/// <summary>
45
54
/// Creates a DbParameter object and adds it to a DbCommand
@@ -93,9 +102,6 @@ public static DbCommand WithSqlParam(this DbCommand cmd, string paramName, SqlPa
93
102
if ( string . IsNullOrEmpty ( cmd . CommandText ) && cmd . CommandType != System . Data . CommandType . StoredProcedure )
94
103
throw new InvalidOperationException ( "Call LoadStoredProc before using this method" ) ;
95
104
96
- //var param = cmd.CreateParameter();
97
- //param.ParameterName = paramName;
98
- //configureParam?.Invoke(param);
99
105
cmd . Parameters . Add ( parameter ) ;
100
106
101
107
return cmd ;
@@ -104,12 +110,10 @@ public static DbCommand WithSqlParam(this DbCommand cmd, string paramName, SqlPa
104
110
public class SprocResults
105
111
{
106
112
107
- // private DbCommand _command;
108
113
private DbDataReader _reader ;
109
114
110
115
public SprocResults ( DbDataReader reader )
111
116
{
112
- // _command = command;
113
117
_reader = reader ;
114
118
}
115
119
@@ -216,13 +220,12 @@ public static void ExecuteStoredProc(this DbCommand command, Action<SprocResults
216
220
using ( var reader = command . ExecuteReader ( commandBehaviour ) )
217
221
{
218
222
var sprocResults = new SprocResults ( reader ) ;
219
- // return new SprocResults();
220
223
handleResults ( sprocResults ) ;
221
224
}
222
225
}
223
226
finally
224
227
{
225
- if ( manageConnection )
228
+ if ( manageConnection )
226
229
{
227
230
command . Connection . Close ( ) ;
228
231
}
@@ -264,75 +267,75 @@ public static void ExecuteStoredProc(this DbCommand command, Action<SprocResults
264
267
}
265
268
}
266
269
}
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
+
337
340
}
338
341
}
0 commit comments