Skip to content

Commit 0bb814a

Browse files
authored
Merge pull request snickler#23 from mooshpot/patch-2
Update EFExtensions.cs
2 parents 008661a + 529fd56 commit 0bb814a

File tree

1 file changed

+98
-22
lines changed

1 file changed

+98
-22
lines changed

EFCoreFluent/src/EFCoreFluent/EFExtensions.cs

Lines changed: 98 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,35 @@
1111
namespace Snickler.EFCore
1212
{
1313
public static class EFExtensions
14-
{/// <summary>
15-
/// Creates an initial DbCommand object based on a stored procedure name
16-
/// </summary>
17-
/// <param name="context">target database context</param>
18-
/// <param name="storedProcName">target procedure name</param>
19-
/// <param name="prependDefaultSchema">Prepend the default schema name to <paramref name="storedProcName"/> if explicitly defined in <paramref name="context"/></param>
20-
/// <returns></returns>
21-
public static DbCommand LoadStoredProc(this DbContext context, string storedProcName, bool prependDefaultSchema = true)
22-
{
23-
var cmd = context.Database.GetDbConnection().CreateCommand();
24-
if (prependDefaultSchema)
25-
{
26-
var schemaName = context.Model.Relational().DefaultSchema;
27-
if (schemaName != null)
28-
{
29-
storedProcName = $"{schemaName}.{storedProcName}";
30-
}
14+
{
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();
3126

32-
}
33-
cmd.CommandText = storedProcName;
34-
cmd.CommandType = System.Data.CommandType.StoredProcedure;
35-
return cmd;
36-
}
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+
}
3743

3844
/// <summary>
3945
/// Creates a DbParameter object and adds it to a DbCommand
@@ -258,5 +264,75 @@ public static void ExecuteStoredProc(this DbCommand command, Action<SprocResults
258264
}
259265
}
260266
}
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+
261337
}
262338
}

0 commit comments

Comments
 (0)