diff --git a/Exemples.sln b/Exemples.sln index 13aae2d..9cfc3e7 100644 --- a/Exemples.sln +++ b/Exemples.sln @@ -539,6 +539,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ex_042_012_EF_CF_Many_to_Ma EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "chap041_EF_Fundamentals", "chap041_EF_Fundamentals", "{56C5A51B-16E9-4F93-9C32-8F91710391E8}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_LISEZ-MOI_ !!", "_LISEZ-MOI_ !!", "{AB06CC3B-6D82-48D1-89FC-14C96C77FE7F}" + ProjectSection(SolutionItems) = preProject + p08_BDD_EntityFramework\ReadMe.md = p08_BDD_EntityFramework\ReadMe.md + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ex_041_001_ConnectionStrings", "p08_BDD_EntityFramework\ex_041_001_ConnectionStrings\ex_041_001_ConnectionStrings.csproj", "{097B0926-6663-4B7E-BBBE-DAA7EE278C7C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -4495,6 +4502,26 @@ Global {A14CDD6E-3E6E-4E7A-9277-3F00C0F658FD}.Release|x64.Build.0 = Release|Any CPU {A14CDD6E-3E6E-4E7A-9277-3F00C0F658FD}.Release|x86.ActiveCfg = Release|Any CPU {A14CDD6E-3E6E-4E7A-9277-3F00C0F658FD}.Release|x86.Build.0 = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|ARM.ActiveCfg = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|ARM.Build.0 = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|ARM64.Build.0 = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|x64.ActiveCfg = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|x64.Build.0 = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|x86.ActiveCfg = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Debug|x86.Build.0 = Debug|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|Any CPU.Build.0 = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|ARM.ActiveCfg = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|ARM.Build.0 = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|ARM64.ActiveCfg = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|ARM64.Build.0 = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|x64.ActiveCfg = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|x64.Build.0 = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|x86.ActiveCfg = Release|Any CPU + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -4751,6 +4778,8 @@ Global {4F8FDCA9-D98C-4137-BD14-C255506209B0} = {593200F9-6D14-43BC-9289-8BB75FAC6552} {A14CDD6E-3E6E-4E7A-9277-3F00C0F658FD} = {593200F9-6D14-43BC-9289-8BB75FAC6552} {56C5A51B-16E9-4F93-9C32-8F91710391E8} = {65DF0C9A-4F06-4361-8DD9-E8360B6325CA} + {AB06CC3B-6D82-48D1-89FC-14C96C77FE7F} = {65DF0C9A-4F06-4361-8DD9-E8360B6325CA} + {097B0926-6663-4B7E-BBBE-DAA7EE278C7C} = {56C5A51B-16E9-4F93-9C32-8F91710391E8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {8D31C3AE-36FF-4667-A2A7-0E670245A59E} diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Nounours.cs b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Nounours.cs new file mode 100644 index 0000000..cb2ff6f --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Nounours.cs @@ -0,0 +1,10 @@ +using System; + +namespace ex_041_001_ConnectionStrings +{ + class Nounours + { + public int Id { get; set; } + public string Nom { get; set; } + } +} diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Program.cs b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Program.cs new file mode 100644 index 0000000..6587676 --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/Program.cs @@ -0,0 +1,34 @@ +using System; + +namespace ex_041_001_ConnectionStrings +{ + class Program + { + static void Main(string[] args) + { + Nounours chewie = new Nounours { Nom = "Chewbacca" }; + Nounours yoda = new Nounours { Nom = "Yoda" }; + Nounours ewok = new Nounours { Nom = "Ewok" }; + + //using (var context = new SqlServerContext()) + //{ + // // 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); + // 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"); + context.Add(chewie); + context.Add(yoda); + context.Add(ewok); + context.SaveChanges(); + } + } + } +} diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ReadMe.md b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ReadMe.md new file mode 100644 index 0000000..246c9bf --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ReadMe.md @@ -0,0 +1,17 @@ +# ex_041_001_ConnectionStrings + +Cet exemple a pour but de présenter les chaîne de connexion (*connection strings*). + +Servent à se connecter à une base de données +diffèrent en fonction des providers +parfois, elles nécessitent login/pwd_ + +dans l'exemple : +2 providers +pour sqlite, il faut rajouter les nugets, le chemin du starting working directory +on peut observer le contenu avec DB browser : https://sqlitebrowser.org/dl/ + +todo : faire fonctionner avec les deux providers +préciser la différence pour les migrations +dotnet ef migrations add ex_041_001_ConnectionStrings --context SQLiteContext +dotnet ef database update --context SQLiteContext \ No newline at end of file diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/SQLiteContext.cs b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/SQLiteContext.cs new file mode 100644 index 0000000..90399b3 --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/SQLiteContext.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Text; + +namespace ex_041_001_ConnectionStrings +{ + class SQLiteContext : DbContext + { + public DbSet Nounours { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlite($"Data Source=Nounours.db"); + } +} diff --git a/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/SqlServerContext.cs b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/SqlServerContext.cs new file mode 100644 index 0000000..8dcc839 --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/SqlServerContext.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; + +namespace ex_041_001_ConnectionStrings +{ + class SqlServerContext : DbContext + { + public DbSet Nounours { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=ex_041_001_ConnectionStrings.Nounours.mdf;Trusted_Connection=True;"); + } +} 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 new file mode 100644 index 0000000..b512e4b --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_001_ConnectionStrings/ex_041_001_ConnectionStrings.csproj @@ -0,0 +1,19 @@ + + + + Exe + netcoreapp3.0 + $(MSBuildProjectDirectory) + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/p08_BDD_EntityFramework/temp.md b/p08_BDD_EntityFramework/temp.md index 6425d03..f64b20d 100644 --- a/p08_BDD_EntityFramework/temp.md +++ b/p08_BDD_EntityFramework/temp.md @@ -1,8 +1,14 @@ #### to do ##### fundamentals (41) * connection strings -* connection resiliency -* testing => sqlite, inmemory + * exemple avec une connection string dans OnConfiguring + * exemple avec une connection string dans AppConfig (WPF) + * exemple avec une connection string appsettings.json (ASP.NET Core) + * exemple avec UWP ? +* connection resiliency ? +* testing + * SQLite in memory + * InMemory * configuring a dbcontext ? * nullable reference types => navigating (include, theninclude) ##### creating a model (42)