1+ using EntityFrameworkExtras . EF6 ;
2+ using System ;
3+ using System . Collections . Generic ;
4+ using System . Data ;
5+ using System . Data . Entity ;
6+ using System . Linq ;
7+ using System . Text ;
8+ using System . Threading . Tasks ;
9+
10+ namespace Z . EntityFrameworkExtras . labEF6
11+ {
12+ class Request_OutputParam
13+ {
14+ public static void Execute ( )
15+ {
16+ // Create BD
17+ using ( var context = new EntityContext ( ) )
18+ {
19+ My . CreateBD ( context ) ;
20+ }
21+
22+ // CLEAN
23+ using ( var context = new EntityContext ( ) )
24+ {
25+ context . EntitySimples . RemoveRange ( context . EntitySimples ) ;
26+
27+ context . SaveChanges ( ) ;
28+ }
29+
30+ // SEED
31+ using ( var context = new EntityContext ( ) )
32+ {
33+ for ( int i = 0 ; i < 3 ; i ++ )
34+ {
35+ context . EntitySimples . Add ( new EntitySimple { ColumnInt = i } ) ;
36+ }
37+
38+ context . SaveChanges ( ) ;
39+ }
40+
41+
42+
43+ // TEST
44+ using ( var context = new EntityContext ( ) )
45+ {
46+ var connection = context . Database . Connection ;
47+ connection . Open ( ) ;
48+ using ( var commande = connection . CreateCommand ( ) )
49+ {
50+ commande . CommandText = @"
51+ if exists (select 1 from sys.procedures where name = 'PROC_Get_EntitySimple')
52+ BEGIN
53+ DROP PROCEDURE [dbo].[PROC_Get_EntitySimple]
54+ END
55+ " ;
56+ commande . ExecuteNonQuery ( ) ;
57+ }
58+
59+ using ( var commande = connection . CreateCommand ( ) )
60+ {
61+ commande . CommandText = @"
62+ CREATE PROCEDURE [dbo].[PROC_Get_EntitySimple]
63+
64+ @ParameterID INT ,
65+ @ParameterInt INT = NULL OUTPUT
66+
67+
68+ AS
69+ BEGIN
70+ Select * from EntitySimples
71+ Where ColumnInt = @ParameterID
72+
73+ Set @ParameterInt = @ParameterID +1
74+ END
75+ " ;
76+ commande . ExecuteNonQuery ( ) ;
77+ }
78+
79+ }
80+
81+ // TEST
82+ using ( var context = new EntityContext ( ) )
83+ {
84+ var proc_Get_EntitySimple = new Proc_Get_EntitySimple ( ) { ParameterID = 2 } ;
85+ var entity = context . Database . ExecuteStoredProcedure < EntitySimple > ( proc_Get_EntitySimple ) ;
86+ var output = proc_Get_EntitySimple . ParameterInt ;
87+ }
88+ }
89+
90+ public class EntityContext : DbContext
91+ {
92+ public EntityContext ( ) : base ( My . ConnectionString )
93+ {
94+ }
95+
96+ public DbSet < EntitySimple > EntitySimples { get ; set ; }
97+
98+ protected override void OnModelCreating ( DbModelBuilder modelBuilder )
99+ {
100+ base . OnModelCreating ( modelBuilder ) ;
101+ }
102+ }
103+
104+ [ StoredProcedure ( "PROC_Get_EntitySimple" ) ]
105+ public class Proc_Get_EntitySimple
106+ {
107+ [ StoredProcedureParameter ( SqlDbType . Int , Direction = ParameterDirection . Output ) ]
108+ public int ParameterInt { get ; set ; }
109+
110+ [ StoredProcedureParameter ( SqlDbType . Int , Direction = ParameterDirection . Input ) ]
111+ public int ParameterID { get ; set ; }
112+ }
113+
114+ public class EntitySimple
115+ {
116+ public int ID { get ; set ; }
117+ public int ColumnInt { get ; set ; }
118+ public String ColumnString { get ; set ; }
119+ }
120+ }
121+ }
0 commit comments