Skip to content

Commit 9864bb3

Browse files
committed
save add lab
1 parent 0fd74bf commit 9864bb3

File tree

9 files changed

+452
-0
lines changed

9 files changed

+452
-0
lines changed

EntityFrameworkExtras.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkExtras.EFCor
4242
EndProject
4343
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkExtras.EFCore3.NetStandard21", "EntityFrameworkExtras.EFCore3.NetStandard21\EntityFrameworkExtras.EFCore3.NetStandard21.csproj", "{909F9FC3-3384-467B-9A7D-1B2328080BA8}"
4444
EndProject
45+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z.EntityFrameworkExtras.labEF6", "Z.EntityFrameworkExtras.labEF6\Z.EntityFrameworkExtras.labEF6.csproj", "{E23EBDE9-6863-4260-95F2-A99C73CE48CC}"
46+
EndProject
4547
Global
4648
GlobalSection(SharedMSBuildProjectFiles) = preSolution
4749
EntityFrameworkExtras.Shared\EntityFrameworkExtras.Shared.projitems*{357c473a-d606-4a9b-8c5f-17deb6ffb006}*SharedItemsImports = 4
@@ -103,6 +105,10 @@ Global
103105
{909F9FC3-3384-467B-9A7D-1B2328080BA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
104106
{909F9FC3-3384-467B-9A7D-1B2328080BA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
105107
{909F9FC3-3384-467B-9A7D-1B2328080BA8}.Release|Any CPU.Build.0 = Release|Any CPU
108+
{E23EBDE9-6863-4260-95F2-A99C73CE48CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
109+
{E23EBDE9-6863-4260-95F2-A99C73CE48CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
110+
{E23EBDE9-6863-4260-95F2-A99C73CE48CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
111+
{E23EBDE9-6863-4260-95F2-A99C73CE48CC}.Release|Any CPU.Build.0 = Release|Any CPU
106112
EndGlobalSection
107113
GlobalSection(SolutionProperties) = preSolution
108114
HideSolutionNode = FALSE
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<configSections>
4+
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
5+
<section name="entityFramework"
6+
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
7+
requirePermission="false"/>
8+
</configSections>
9+
<startup>
10+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
11+
</startup>
12+
<entityFramework>
13+
<providers>
14+
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
15+
</providers>
16+
</entityFramework>
17+
</configuration>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Entity;
4+
using System.Data.SqlClient;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Z.EntityFrameworkExtras.labEF6
10+
{
11+
class My
12+
{
13+
public static string DataBaseName = "LabExtra";
14+
15+
public static string ConnectionString =
16+
("Server=[REPLACE];Initial Catalog = [BD]; Integrated Security = true; Connection Timeout = 300; Persist Security Info=True").Replace("[REPLACE]", Environment.MachineName).Replace("[BD]", DataBaseName);
17+
18+
public static void DeleteBD(DbContext context)
19+
{
20+
context.Database.Delete();
21+
}
22+
23+
public static void CreateBD(DbContext context)
24+
{
25+
try
26+
{
27+
context.Database.CreateIfNotExists();
28+
if (!context.Database.CompatibleWithModel(throwIfNoMetadata: true))
29+
{
30+
throw new Exception("Delete and Create DataBase");
31+
}
32+
}
33+
catch
34+
{
35+
try
36+
{
37+
My.DeleteBD(context);
38+
}
39+
catch (Exception e)
40+
{
41+
using (var commande = new SqlCommand("ALTER DATABASE " + DataBaseName + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE;DROP DATABASE " + DataBaseName + " ;", new SqlConnection(My.ConnectionString)))
42+
{
43+
commande.Connection.Open();
44+
commande.ExecuteNonQuery();
45+
}
46+
}
47+
context.Database.CreateIfNotExists();
48+
}
49+
}
50+
}
51+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Z.EntityFrameworkExtras.labEF6
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
Request_OutputParam.Execute();
14+
}
15+
}
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// Les informations générales relatives à un assembly dépendent de
6+
// l'ensemble d'attributs suivant. Changez les valeurs de ces attributs pour modifier les informations
7+
// associées à un assembly.
8+
[assembly: AssemblyTitle("Z.EntityFrameworkExtras.labEF6")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Z.EntityFrameworkExtras.labEF6")]
13+
[assembly: AssemblyCopyright("Copyright © 2020")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// L'affectation de la valeur false à ComVisible rend les types invisibles dans cet assembly
18+
// aux composants COM. Si vous devez accéder à un type dans cet assembly à partir de
19+
// COM, affectez la valeur true à l'attribut ComVisible sur ce type.
20+
[assembly: ComVisible(false)]
21+
22+
// Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM
23+
[assembly: Guid("e23ebde9-6863-4260-95f2-a99c73ce48cc")]
24+
25+
// Les informations de version pour un assembly se composent des quatre valeurs suivantes :
26+
//
27+
// Version principale
28+
// Version secondaire
29+
// Numéro de build
30+
// Révision
31+
//
32+
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
33+
// en utilisant '*', comme indiqué ci-dessous :
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
public class Template
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+
}
87+
}
88+
89+
public class EntityContext : DbContext
90+
{
91+
public EntityContext() : base(My.ConnectionString)
92+
{
93+
}
94+
95+
public DbSet<EntitySimple> EntitySimples { get; set; }
96+
97+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
98+
{
99+
base.OnModelCreating(modelBuilder);
100+
}
101+
}
102+
103+
[StoredProcedure("PROC_Get_EntitySimple")]
104+
public class Proc_Get_EntitySimple
105+
{
106+
[StoredProcedureParameter(SqlDbType.Int, Direction = ParameterDirection.Output)]
107+
public int ParameterInt { get; set; }
108+
109+
[StoredProcedureParameter(SqlDbType.Int, Direction = ParameterDirection.Input)]
110+
public int ParameterID { get; set; }
111+
}
112+
113+
public class EntitySimple
114+
{
115+
public int ID { get; set; }
116+
public int ColumnInt { get; set; }
117+
public String ColumnString { get; set; }
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)