From 8e0008096b43e698c96a06a325790e5c6b55ec8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Chevaldonn=C3=A9?= Date: Thu, 9 Jan 2020 11:53:41 +0100 Subject: [PATCH] updated ex_041_001 --- .../20200109092359_ex_041_001.Designer.cs | 36 +++++++++++++++++++ .../Migrations/20200109092359_ex_041_001.cs | 29 +++++++++++++++ .../Migrations/SQLiteContextModelSnapshot.cs | 34 ++++++++++++++++++ .../ex_041_001_ConnectionStrings/Program.cs | 36 +++++++++++++++---- .../ex_041_001_ConnectionStrings/ReadMe.md | 36 +++++++++++++++---- .../ex_041_001_ConnectionStrings.csproj | 2 +- 6 files changed, 159 insertions(+), 14 deletions(-) create mode 100644 p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/20200109092359_ex_041_001.Designer.cs create mode 100644 p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/20200109092359_ex_041_001.cs create mode 100644 p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/SQLiteContextModelSnapshot.cs diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/20200109092359_ex_041_001.Designer.cs b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/20200109092359_ex_041_001.Designer.cs new file mode 100644 index 0000000..8c389cd --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/20200109092359_ex_041_001.Designer.cs @@ -0,0 +1,36 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using ex_041_001_ConnectionStrings; + +namespace ex_041_001_ConnectionStrings.Migrations +{ + [DbContext(typeof(SQLiteContext))] + [Migration("20200109092359_ex_041_001")] + partial class ex_041_001 + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.1.0"); + + modelBuilder.Entity("ex_041_001_ConnectionStrings.Nounours", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Nom") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Nounours"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/20200109092359_ex_041_001.cs b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/20200109092359_ex_041_001.cs new file mode 100644 index 0000000..71a3d83 --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/20200109092359_ex_041_001.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace ex_041_001_ConnectionStrings.Migrations +{ + public partial class ex_041_001 : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Nounours", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Nom = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Nounours", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Nounours"); + } + } +} diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/SQLiteContextModelSnapshot.cs b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/SQLiteContextModelSnapshot.cs new file mode 100644 index 0000000..ceb4779 --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Migrations/SQLiteContextModelSnapshot.cs @@ -0,0 +1,34 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using ex_041_001_ConnectionStrings; + +namespace ex_041_001_ConnectionStrings.Migrations +{ + [DbContext(typeof(SQLiteContext))] + partial class SQLiteContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.1.0"); + + modelBuilder.Entity("ex_041_001_ConnectionStrings.Nounours", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Nom") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Nounours"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Program.cs b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Program.cs index 875fb65..98a6664 100644 --- a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Program.cs +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; namespace ex_041_001_ConnectionStrings { @@ -10,10 +11,32 @@ namespace ex_041_001_ConnectionStrings Nounours yoda = new Nounours { Nom = "Yoda" }; Nounours ewok = new Nounours { Nom = "Ewok" }; - using (var context = new SqlServerContext()) + if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + using (var context = new SqlServerContext()) + { + // Crée des nounours et les insère dans la base + Console.WriteLine("Creates and inserts new Nounours with SqlServer"); + context.Add(chewie); + context.Add(yoda); + context.Add(ewok); + context.SaveChanges(); + } + + using (var context = new SqlServerContext()) + { + foreach(var n in context.Nounours) + { + Console.WriteLine($"{n.Id} - {n.Nom}"); + } + context.SaveChanges(); + } + } + + using (var context = new SQLiteContext()) { // Crée des nounours et les insère dans la base - Console.WriteLine("Creates and inserts new Nounours"); + Console.WriteLine("Creates and inserts new Nounours with SQLite"); context.Add(chewie); context.Add(yoda); context.Add(ewok); @@ -22,11 +45,10 @@ namespace ex_041_001_ConnectionStrings using (var context = new SQLiteContext()) { - // Crée des nounours et les insère dans la base - Console.WriteLine("Creates and inserts new Nounours"); - context.Add(chewie); - context.Add(yoda); - context.Add(ewok); + foreach(var n in context.Nounours) + { + Console.WriteLine($"{n.Id} - {n.Nom}"); + } context.SaveChanges(); } } diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ReadMe.md b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ReadMe.md index c960f6b..aefcc6e 100644 --- a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ReadMe.md +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ReadMe.md @@ -1,5 +1,6 @@ # ex_041_001_ConnectionStrings -*31/12/2019 ⋅ Marc Chevaldonné* +*31/12/2019 ⋅ Marc Chevaldonné* +*Dernière mise à jour : 09/01/2020 ⋅ Marc Chevaldonné* --- @@ -42,7 +43,8 @@ C'est tout ce que cet exemple souhaite mettre en valeur : les chaînes de connex ## Comment générer et exécuter l'exemple ? Pour générer l'exemple, il vous faut d'abord préparer les migrations et les tables. -* Ouvrez la *Console du Gestionnaire de package*, pour cela, dirigez-vous dans le menu *Outils*, puis *Gestionnaire de package NuGet*, puis *Console du Gestionnaire de package*. +* (__Windows__ Visual Studio 2019) : ouvrez la *Console du Gestionnaire de package*, pour cela, dirigez-vous dans le menu *Outils*, puis *Gestionnaire de package NuGet*, puis *Console du Gestionnaire de package*. +* (__MacOSX__ Visual Studio 2019 For Mac) : ouvrez un Terminal. * Dans la console que vous venez d'ouvrir, déplacez-vous dans le dossier du projet, ici : ``` cd .\p08_BDD_EntityFramework\ex_041_001_ConnectionStrings @@ -66,23 +68,45 @@ mais comme ici, nous sommes dans le cas particulier où nous avons deux contexte dotnet ef migrations add ex_041_001_SqlServer --context SqlServerContext dotnet ef migrations add ex_041_001_SQLite --context SQLiteContext ``` +*Note : sous MacOSX, n'utilisez que SQLite, soit :* +``` +dotnet ef migrations add ex_041_001_SQLite --context SQLiteContext +``` ### Création des tables Tapez ensuite les commandes suivantes : ``` dotnet ef database update --context SqlServerContext dotnet ef database update --context SQLiteContext ``` +*Note : sous MacOSX, n'utilisez que SQLite, soit :* +``` +dotnet ef database update --context SQLiteContext +``` ### Génération et exécution Vous pouvez maintenant générer et exécuter l'exemple. Le résultat de l'exécution peut donner : +* sur Windows : +``` +Creates and inserts new Nounours with SqlServer +1 - Chewbacca +2 - Yoda +3 - Ewok +Creates and inserts new Nounours with SQLite +1 - Chewbacca +2 - Yoda +3 - Ewok +``` +* sur MacOSX : ``` -Creates and inserts new Nounours -Creates and inserts new Nounours +Creates and inserts new Nounours with SQLite +1 - Chewbacca +2 - Yoda +3 - Ewok ``` ## Comment vérifier le contenu des bases de données SQL Server et SQLite ? -### SqlServer +### SqlServer (seulement sur Windows) Vous pouvez vérifier le contenu de votre base en utilisant l'*Explorateur d'objets SQL Server*. * Pour cela, allez dans le menu *Affichage* puis *Explorateur d'objets SQL Server*. @@ -106,7 +130,7 @@ Vous pouvez vérifier le contenu de votre base en utilisant l'*Explorateur d'obj Notez qu'il est également possible d'utiliser l'*Explorateur d'objets SQL Server* pour ajouter, modifier ou supprimer des données dans les tables. -### SQLite +### SQLite (Windows et MacOSX) Pour vérifier le contenu de votre base SQLite, vous pouvez utiliser le programme *DB Browser* : * Rendez-vous sur la page : https://sqlitebrowser.org/dl/ et téléchargez le programme *DB Browser*. * Lancez *DB Browser for SQLite* diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ex_041_001_ConnectionStrings.csproj b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ex_041_001_ConnectionStrings.csproj index 0fb7958..f6bd7c1 100644 --- a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ex_041_001_ConnectionStrings.csproj +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ex_041_001_ConnectionStrings.csproj @@ -9,6 +9,6 @@ - +