Skip to content

Commit bfd1e98

Browse files
committed
save
1 parent 2dd2951 commit bfd1e98

File tree

5 files changed

+150
-2
lines changed

5 files changed

+150
-2
lines changed

EntityFrameworkExtras.EFCore3.lab/My.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class My
1515

1616
public static string ConnectionString =
1717
("Server=[REPLACE];Initial Catalog = [BD]; Integrated Security = true; Connection Timeout = 300; Persist Security Info=True").Replace("[REPLACE]", Environment.MachineName).Replace("[BD]", DataBaseName);
18+
public static string ConnectionStringTimeOut =
19+
("Server=[REPLACE];Initial Catalog = [BD]; Integrated Security = true; Connection Timeout = 5; Persist Security Info=True").Replace("[REPLACE]", Environment.MachineName).Replace("[BD]", DataBaseName);
1820

1921
public static void DeleteBD(DbContext context)
2022
{

EntityFrameworkExtras.EFCore3.lab/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Program
66
{
77
static void Main(string[] args)
88
{
9-
Request_Async.Execute();
9+
Request_TimeOut.Execute();
1010
}
1111
}
1212
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Transactions;
7+
using EntityFrameworkExtras.EFCore;
8+
using Microsoft.Data.SqlClient;
9+
using Microsoft.EntityFrameworkCore;
10+
11+
namespace Z.EntityFrameworkExtras.Lab.EFCore30
12+
{
13+
class Request_TimeOut
14+
{
15+
public static void Execute()
16+
{
17+
// Create BD
18+
using (var context = new EntityContext())
19+
{
20+
My.CreateBD(context);
21+
}
22+
23+
// CLEAN
24+
using (var context = new EntityContext())
25+
{
26+
context.EntitySimples.RemoveRange(context.EntitySimples);
27+
28+
context.SaveChanges();
29+
}
30+
31+
// SEED
32+
using (var context = new EntityContext())
33+
{
34+
for (int i = 0; i < 3; i++)
35+
{
36+
context.EntitySimples.Add(new EntitySimple { ColumnInt = i });
37+
}
38+
39+
context.SaveChanges();
40+
}
41+
42+
43+
44+
// TEST
45+
using (var context = new EntityContext())
46+
{
47+
var connection = context.Database.GetDbConnection();
48+
connection.Open();
49+
using (var commande = connection.CreateCommand())
50+
{
51+
commande.CommandText = @"
52+
if exists (select 1 from sys.procedures where name = 'PROC_Get_EntitySimple')
53+
BEGIN
54+
DROP PROCEDURE [dbo].[PROC_Get_EntitySimple]
55+
END
56+
";
57+
commande.ExecuteNonQuery();
58+
}
59+
60+
using (var commande = connection.CreateCommand())
61+
{
62+
commande.CommandText = @"
63+
CREATE PROCEDURE [dbo].[PROC_Get_EntitySimple]
64+
65+
@ParameterID INT ,
66+
@ParameterInt INT = NULL OUTPUT
67+
68+
69+
AS
70+
BEGIN
71+
WAITFOR DELAY '00:00:08';
72+
update EntitySimples
73+
Set ColumnInt = @ParameterID ;
74+
75+
Set @ParameterInt = @ParameterID +1
76+
END
77+
";
78+
commande.ExecuteNonQuery();
79+
}
80+
81+
}
82+
83+
// TEST
84+
using (var context = new EntityContext())
85+
{
86+
var proc_Get_EntitySimple = new Proc_Get_EntitySimple() { ParameterID = 2 };
87+
context.Database.SetCommandTimeout(5);
88+
context.Database.ExecuteStoredProcedure(proc_Get_EntitySimple);
89+
90+
91+
var t = context.Database.ExecuteStoredProcedure<EntitySimple>(proc_Get_EntitySimple);
92+
93+
// var list = context.EntitySimples.ToList();
94+
}
95+
}
96+
97+
public class EntityContext : DbContext
98+
{
99+
public EntityContext() : base()
100+
{
101+
}
102+
103+
public DbSet<EntitySimple> EntitySimples { get; set; }
104+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
105+
{
106+
optionsBuilder.UseSqlServer(new SqlConnection(My.ConnectionStringTimeOut));
107+
108+
base.OnConfiguring(optionsBuilder);
109+
}
110+
111+
}
112+
113+
[StoredProcedure("PROC_Get_EntitySimple")]
114+
public class Proc_Get_EntitySimple
115+
{
116+
[StoredProcedureParameter(SqlDbType.Int, Direction = ParameterDirection.Output)]
117+
public int ParameterInt { get; set; }
118+
119+
[StoredProcedureParameter(SqlDbType.Int, Direction = ParameterDirection.Input)]
120+
public int ParameterID { get; set; }
121+
}
122+
123+
124+
public class EntitySimple
125+
{
126+
public int ID { get; set; }
127+
public int ColumnInt { get; set; }
128+
public String ColumnString { get; set; }
129+
}
130+
}
131+
}

EntityFrameworkExtras.Shared/DatabaseExtensions.EFCore2x.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public static IEnumerable<T> ExecuteStoredProcedure<T>(this DatabaseFacade datab
5353
using (var command = database.GetDbConnection().CreateCommand())
5454
{
5555
command.CommandText = info.Sql;
56+
int? commandTimeout = database.GetCommandTimeout();
57+
if (commandTimeout.HasValue)
58+
{
59+
command.CommandTimeout = commandTimeout.Value;
60+
}
5661
command.CommandType = CommandType.Text;
5762
command.Parameters.AddRange(info.SqlParameters);
5863
command.Transaction = database.CurrentTransaction?.GetDbTransaction();

EntityFrameworkExtras.Shared/DatabaseExtensions.EFCore3x.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void ExecuteStoredProcedure(this DatabaseFacade database, object s
2626

2727
var info = StoredProcedureParser.BuildStoredProcedureInfo(storedProcedure);
2828

29-
database.ExecuteSqlRawAsync(info.Sql, info.SqlParameters);
29+
database.ExecuteSqlRaw(info.Sql, info.SqlParameters);
3030

3131
SetOutputParameterValues(info.SqlParameters, storedProcedure);
3232
}
@@ -78,6 +78,11 @@ public static IEnumerable<T> ExecuteStoredProcedure<T>(this DatabaseFacade datab
7878
using (var command = database.GetDbConnection().CreateCommand())
7979
{
8080
command.CommandText = info.Sql;
81+
int? commandTimeout = database.GetCommandTimeout();
82+
if (commandTimeout.HasValue)
83+
{
84+
command.CommandTimeout = commandTimeout.Value;
85+
}
8186
command.CommandType = CommandType.Text;
8287
command.Parameters.AddRange(info.SqlParameters);
8388
command.Transaction = database.CurrentTransaction?.GetDbTransaction();
@@ -133,6 +138,11 @@ public static async Task<IEnumerable<T>> ExecuteStoredProcedureAsync<T>(this Dat
133138
using (var command = database.GetDbConnection().CreateCommand())
134139
{
135140
command.CommandText = info.Sql;
141+
int? commandTimeout = database.GetCommandTimeout();
142+
if (commandTimeout.HasValue)
143+
{
144+
command.CommandTimeout = commandTimeout.Value;
145+
}
136146
command.CommandType = CommandType.Text;
137147
command.Parameters.AddRange(info.SqlParameters);
138148
command.Transaction = database.CurrentTransaction?.GetDbTransaction();

0 commit comments

Comments
 (0)