Skip to content

Commit da0205a

Browse files
Add a lab
1 parent ff66b31 commit da0205a

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

Z.EntityFrameworkExtras.Lab.EFCore9/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ internal class Program
44
{
55
static void Main(string[] args)
66
{
7-
Request_storeProcedure_Transaction.Execute();
7+
Request_storeProcedure_DateTimeOnly.Execute();
88
}
99
}
1010
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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.EFCore9
14+
{
15+
class Request_storeProcedure_DateTimeOnly
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+
52+
using (var commande = connection.CreateCommand())
53+
{
54+
commande.CommandText = @"
55+
if exists (select 1 from sys.procedures where name = 'PROC_Get_EntitySimple')
56+
BEGIN
57+
DROP PROCEDURE [dbo].[PROC_Get_EntitySimple]
58+
END
59+
";
60+
commande.ExecuteNonQuery();
61+
}
62+
63+
using (var commande = connection.CreateCommand())
64+
{
65+
commande.CommandText = @"
66+
CREATE PROCEDURE [dbo].[PROC_Get_EntitySimple]
67+
68+
@ParameterID INT ,
69+
@DateOnly DATE,
70+
@TimeOnly TIME(7),
71+
@ParameterInt INT = NULL OUTPUT
72+
73+
74+
AS
75+
BEGIN
76+
update EntitySimples
77+
Set ColumnInt = @ParameterID ;
78+
79+
Set @ParameterInt = @ParameterID +1
80+
END
81+
";
82+
commande.ExecuteNonQuery();
83+
}
84+
}
85+
86+
// TEST
87+
using (var context = new EntityContext())
88+
{
89+
var proc_Get_EntitySimple = new Proc_Get_EntitySimple() { ParameterID = 2, DateOnly = DateOnly.MinValue, TimeOnly = TimeOnly.MinValue };
90+
91+
using (var tran = new TransactionScope())
92+
{
93+
context.Database.ExecuteStoredProcedure(proc_Get_EntitySimple);
94+
}
95+
96+
var list2 = context.Database.ExecuteStoredProcedure<EntitySimple>(proc_Get_EntitySimple);
97+
98+
context.Database.ExecuteStoredProcedure(proc_Get_EntitySimple);
99+
100+
101+
var list = context.EntitySimples.ToList();
102+
}
103+
}
104+
105+
public class EntityContext : DbContext
106+
{
107+
public EntityContext() : base( )
108+
{
109+
}
110+
111+
public DbSet<EntitySimple> EntitySimples { get; set; }
112+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
113+
{
114+
optionsBuilder.UseSqlServer(new SqlConnection(My.ConnectionString));
115+
116+
base.OnConfiguring(optionsBuilder);
117+
}
118+
119+
}
120+
121+
[StoredProcedure("PROC_Get_EntitySimple")]
122+
public class Proc_Get_EntitySimple
123+
{
124+
[StoredProcedureParameter(SqlDbType.Int, Direction = ParameterDirection.Output)]
125+
public int ParameterInt { get; set; }
126+
127+
[StoredProcedureParameter(SqlDbType.Int, Direction = ParameterDirection.Input)]
128+
public int ParameterID { get; set; }
129+
[StoredProcedureParameter(SqlDbType.Date, Direction = ParameterDirection.Input)]
130+
public DateOnly DateOnly { get; set; }
131+
[StoredProcedureParameter(SqlDbType.Time, Direction = ParameterDirection.Input)]
132+
public TimeOnly TimeOnly { get; set; }
133+
}
134+
135+
public class EntitySimple
136+
{
137+
public int ID { get; set; }
138+
public int ColumnInt { get; set; }
139+
public String? ColumnString { get; set; }
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)