Skip to content

Commit 372d083

Browse files
committed
save
1 parent 1217fdc commit 372d083

File tree

9 files changed

+463
-1
lines changed

9 files changed

+463
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.SqlClient;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.Data.SqlClient;
8+
using Microsoft.EntityFrameworkCore;
9+
10+
namespace Z.EntityFrameworkExtras.Lab.EFCore30
11+
{
12+
class My
13+
{
14+
public static string DataBaseName = "LabExtra";
15+
16+
public static string ConnectionString =
17+
("Server=[REPLACE];Initial Catalog = [BD]; Integrated Security = true; Connection Timeout = 300; Persist Security Info=True").Replace("[REPLACE]", Environment.MachineName).Replace("[BD]", DataBaseName);
18+
19+
public static void DeleteBD(DbContext context)
20+
{
21+
context.Database.EnsureDeleted();
22+
}
23+
24+
public static void CreateBD(DbContext context)
25+
{
26+
context.Database.EnsureDeleted();
27+
context.Database.EnsureCreated();
28+
}
29+
}
30+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Z.EntityFrameworkExtras.Lab.EFCore30
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Request_storeProcedure_Transaction.Execute();
10+
}
11+
}
12+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\EntityFrameworkExtras.EFCore3.NetStandard21\EntityFrameworkExtras.EFCore3.NetStandard21.csproj" />
14+
</ItemGroup>
15+
16+
</Project>

EntityFrameworkExtras.Shared/DatabaseExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
using System.Data.Common;
1212
using System.Data.SqlClient;
1313
using Microsoft.EntityFrameworkCore;
14+
using Microsoft.EntityFrameworkCore.Storage;
1415
using Microsoft.EntityFrameworkCore.Infrastructure;
1516
#elif EFCORE
1617
using System.Data.Common;
1718
using Microsoft.Data.SqlClient;
1819
using Microsoft.EntityFrameworkCore;
20+
using Microsoft.EntityFrameworkCore.Storage;
1921
using Microsoft.EntityFrameworkCore.Infrastructure;
2022
#endif
2123

@@ -80,6 +82,7 @@ public static IEnumerable<T> ExecuteStoredProcedure<T>(this DatabaseFacade datab
8082
command.CommandText = info.Sql;
8183
command.CommandType = CommandType.Text;
8284
command.Parameters.AddRange(info.SqlParameters);
85+
command.Transaction = database.CurrentTransaction?.GetDbTransaction();
8386
database.OpenConnection();
8487

8588
using (var resultReader = command.ExecuteReader())

EntityFrameworkExtras.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkExtras.EF5.N
4848
EndProject
4949
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkExtras.EF6.Net40", "EntityFrameworkExtras.EF6.Net40\EntityFrameworkExtras.EF6.Net40.csproj", "{7ED1D61C-9396-4663-952D-D2F7DAD19B18}"
5050
EndProject
51+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z.EntityFrameworkExtras.Lab.EFCore30", "EntityFrameworkExtras.EFCore3.lab\Z.EntityFrameworkExtras.Lab.EFCore30.csproj", "{F213770C-7433-44F6-A44E-EC9E3A304BDC}"
52+
EndProject
5153
Global
5254
GlobalSection(SharedMSBuildProjectFiles) = preSolution
5355
EntityFrameworkExtras.Shared\EntityFrameworkExtras.Shared.projitems*{357c473a-d606-4a9b-8c5f-17deb6ffb005}*SharedItemsImports = 4
@@ -123,6 +125,10 @@ Global
123125
{7ED1D61C-9396-4663-952D-D2F7DAD19B18}.Debug|Any CPU.Build.0 = Debug|Any CPU
124126
{7ED1D61C-9396-4663-952D-D2F7DAD19B18}.Release|Any CPU.ActiveCfg = Release|Any CPU
125127
{7ED1D61C-9396-4663-952D-D2F7DAD19B18}.Release|Any CPU.Build.0 = Release|Any CPU
128+
{F213770C-7433-44F6-A44E-EC9E3A304BDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
129+
{F213770C-7433-44F6-A44E-EC9E3A304BDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
130+
{F213770C-7433-44F6-A44E-EC9E3A304BDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
131+
{F213770C-7433-44F6-A44E-EC9E3A304BDC}.Release|Any CPU.Build.0 = Release|Any CPU
126132
EndGlobalSection
127133
GlobalSection(SolutionProperties) = preSolution
128134
HideSolutionNode = FALSE

Z.EntityFrameworkExtras.labEF6/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Program
1010
{
1111
static void Main(string[] args)
1212
{
13-
Request_Enumerable.Execute();
13+
Request_storeProcedure_Transaction.Execute();
1414
}
1515
}
1616
}

0 commit comments

Comments
 (0)