1+ using  System ; 
2+ using  System . Collections . Generic ; 
3+ using  System . Data ;  
4+ using  System . Linq ; 
5+ using  System . Text ; 
6+ using  System . Threading . Tasks ; 
7+ using  System . Transactions ; 
8+ using  EntityFrameworkExtras . EFCore ; 
9+ using  Microsoft . Data . SqlClient ; 
10+ using  Microsoft . EntityFrameworkCore ; 
11+ using  Microsoft . EntityFrameworkCore . Storage ; 
12+ 
13+ namespace  Z . EntityFrameworkExtras . Lab . EFCore30 
14+ { 
15+ 	class  Request_storeProcedure_Transaction 
16+ 	{ 
17+ 		public  static   void  Execute ( ) 
18+ 		{ 
19+ 			// Create BD  
20+ 			using  ( var  context  =  new  EntityContext ( ) ) 
21+ 			{ 
22+ 				My . CreateBD ( context ) ; 
23+ 			} 
24+ 
25+ 			// CLEAN   
26+ 			using  ( var  context  =  new  EntityContext ( ) ) 
27+ 			{ 
28+ 				context . EntitySimples . RemoveRange ( context . EntitySimples ) ; 
29+ 
30+ 				context . SaveChanges ( ) ; 
31+ 			} 
32+ 
33+ 			// SEED   
34+ 			using  ( var  context  =  new  EntityContext ( ) ) 
35+ 			{ 
36+ 				for  ( int  i  =  0 ;  i  <  3 ;  i ++ ) 
37+ 				{ 
38+ 					context . EntitySimples . Add ( new  EntitySimple  {  ColumnInt  =  i  } ) ; 
39+ 				} 
40+ 
41+ 				context . SaveChanges ( ) ; 
42+ 			} 
43+ 
44+ 
45+ 
46+ 			// TEST   
47+ 			using  ( var  context  =  new  EntityContext ( ) ) 
48+ 			{ 
49+ 				var  connection  =  context . Database . GetDbConnection ( ) ; 
50+ 				connection . Open ( ) ; 
51+ 				using  ( var  commande  =  connection . CreateCommand ( ) ) 
52+ 				{ 
53+ 					commande . CommandText  =  @"    
54+ if exists (select 1 from sys.procedures where name = 'PROC_Get_EntitySimple') 
55+ BEGIN 
56+ DROP PROCEDURE [dbo].[PROC_Get_EntitySimple] 
57+ END  
58+ 						" ; 
59+ 					commande . ExecuteNonQuery ( ) ; 
60+ 				} 
61+ 
62+ 				using  ( var  commande  =  connection . CreateCommand ( ) ) 
63+ 				{ 
64+ 					commande . CommandText  =  @"     
65+ CREATE PROCEDURE [dbo].[PROC_Get_EntitySimple] 
66+ 
67+ 	@ParameterID INT 	, 
68+ 	@ParameterInt INT = NULL OUTPUT  
69+ 
70+ 
71+ AS 
72+ BEGIN  
73+ update EntitySimples 
74+ Set ColumnInt = @ParameterID ; 
75+ 
76+ Set @ParameterInt = @ParameterID +1  
77+ END 
78+ 						" ; 
79+ 					commande . ExecuteNonQuery ( ) ; 
80+ 				} 
81+ 
82+ 				using  ( var  commande  =  connection . CreateCommand ( ) ) 
83+ 				{ 
84+ 					commande . CommandText  =  @"     
85+ CREATE PROCEDURE [dbo].[PROC_Get_EntitySimple2] 
86+ 
87+ 	@ParameterID INT 	, 
88+ 	@ParameterInt INT = NULL OUTPUT  
89+ 
90+ 
91+ AS 
92+ BEGIN  
93+ update EntitySimples 
94+ Set ColumnInt = @ParameterID ; 
95+ 
96+ Set @ParameterInt = @ParameterID +1  
97+ 
98+ select * from EntitySimples 
99+ END 
100+ 						" ; 
101+ 					commande . ExecuteNonQuery ( ) ;  
102+ 				} 
103+ 
104+ 			} 
105+ 
106+ 			// TEST   
107+ 			using  ( var  context  =  new  EntityContext ( ) ) 
108+ 			{ 
109+ 				var  proc_Get_EntitySimple  =  new  Proc_Get_EntitySimple ( )  {  ParameterID  =  2  } ; 
110+ 
111+ 				using  ( var  tran  =  new  TransactionScope ( ) ) 
112+ 				{ 
113+ 					context . Database . ExecuteStoredProcedure ( proc_Get_EntitySimple ) ; 
114+ 				} 
115+ 
116+ 				var  transaction  =  context . Database . BeginTransaction ( ) ; 
117+ 
118+ 				context . Database . ExecuteStoredProcedure ( proc_Get_EntitySimple ) ; 
119+ 
120+ 				transaction . Rollback ( ) ; 
121+ 
122+ 				var  list  =  context . EntitySimples . ToList ( ) ; 
123+ 			} 
124+ 
125+ 			// TEST   
126+ 			using  ( var  context  =  new  EntityContext ( ) ) 
127+ 			{ 
128+ 				var  proc_Get_EntitySimple  =  new  Proc_Get_EntitySimple2 ( )  {  ParameterID  =  2  } ; 
129+ 
130+ 				using  ( var  tran  =  new  TransactionScope ( ) ) 
131+ 				{ 
132+ 					context . Database . ExecuteStoredProcedure < EntitySimple > ( proc_Get_EntitySimple ) ; 
133+ 				} 
134+ 
135+ 				var  transaction  =  context . Database . BeginTransaction ( ) ;  
136+ 				context . Database . ExecuteStoredProcedure < EntitySimple > ( proc_Get_EntitySimple ) ; 
137+ 
138+ 				transaction . Rollback ( ) ; 
139+ 
140+ 				var  list  =  context . EntitySimples . ToList ( ) ; 
141+ 			} 
142+ 
143+ 			using  ( var  context  =  new  EntityContext ( ) ) 
144+ 			{ 
145+ 				var  proc_Get_EntitySimple  =  new  Proc_Get_EntitySimple ( )  {  ParameterID  =  2  } ; 
146+ 				using  ( var  tran  =  new  TransactionScope ( ) ) 
147+ 				{ 
148+ 					context . Database . ExecuteStoredProcedure < EntitySimple > ( proc_Get_EntitySimple ) ; 
149+ 					tran . Complete ( ) ; 
150+ 				} 
151+ 
152+ 				var  list  =  context . EntitySimples . ToList ( ) ; 
153+ 			} 
154+ 		} 
155+ 
156+ 		public  class  EntityContext  :  DbContext 
157+ 		{ 
158+ 			public  EntityContext ( )  :  base (  ) 
159+ 			{ 
160+ 			} 
161+ 
162+ 			public  DbSet < EntitySimple >  EntitySimples  {  get ;  set ;  } 
163+ 			protected  override  void  OnConfiguring ( DbContextOptionsBuilder  optionsBuilder ) 
164+ 			{ 
165+ 				optionsBuilder . UseSqlServer ( new  SqlConnection ( My . ConnectionString ) ) ; 
166+ 
167+ 				base . OnConfiguring ( optionsBuilder ) ; 
168+ 			} 
169+ 
170+ 		} 
171+ 
172+ 		[ StoredProcedure ( "PROC_Get_EntitySimple" ) ] 
173+ 		public  class  Proc_Get_EntitySimple 
174+ 		{ 
175+ 			[ StoredProcedureParameter ( SqlDbType . Int ,  Direction  =  ParameterDirection . Output ) ] 
176+ 			public  int  ParameterInt  {  get ;  set ;  } 
177+ 
178+ 			[ StoredProcedureParameter ( SqlDbType . Int ,  Direction  =  ParameterDirection . Input ) ] 
179+ 			public  int  ParameterID  {  get ;  set ;  } 
180+ 		} 
181+ 
182+ 		[ StoredProcedure ( "PROC_Get_EntitySimple2" ) ] 
183+ 		public  class  Proc_Get_EntitySimple2 
184+ 		{ 
185+ 			[ StoredProcedureParameter ( SqlDbType . Int ,  Direction  =  ParameterDirection . Output ) ] 
186+ 			public  int  ParameterInt  {  get ;  set ;  } 
187+ 
188+ 			[ StoredProcedureParameter ( SqlDbType . Int ,  Direction  =  ParameterDirection . Input ) ] 
189+ 			public  int  ParameterID  {  get ;  set ;  } 
190+ 		} 
191+ 
192+ 		public  class  EntitySimple 
193+ 		{ 
194+ 			public  int  ID  {  get ;  set ;  } 
195+ 			public  int  ColumnInt  {  get ;  set ;  } 
196+ 			public  String  ColumnString  {  get ;  set ;  } 
197+ 		} 
198+ 	} 
199+ } 
0 commit comments