begun sample about connection strings; to be finished

EFCore3_Reforged
Marc CHEVALDONNE 6 years ago
parent b47b8956f2
commit a0b861dd73

@ -539,6 +539,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ex_042_012_EF_CF_Many_to_Ma
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "chap041_EF_Fundamentals", "chap041_EF_Fundamentals", "{56C5A51B-16E9-4F93-9C32-8F91710391E8}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "chap041_EF_Fundamentals", "chap041_EF_Fundamentals", "{56C5A51B-16E9-4F93-9C32-8F91710391E8}"
EndProject 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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|x64.Build.0 = Release|Any CPU
{A14CDD6E-3E6E-4E7A-9277-3F00C0F658FD}.Release|x86.ActiveCfg = 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 {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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -4751,6 +4778,8 @@ Global
{4F8FDCA9-D98C-4137-BD14-C255506209B0} = {593200F9-6D14-43BC-9289-8BB75FAC6552} {4F8FDCA9-D98C-4137-BD14-C255506209B0} = {593200F9-6D14-43BC-9289-8BB75FAC6552}
{A14CDD6E-3E6E-4E7A-9277-3F00C0F658FD} = {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} {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 EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8D31C3AE-36FF-4667-A2A7-0E670245A59E} SolutionGuid = {8D31C3AE-36FF-4667-A2A7-0E670245A59E}

@ -0,0 +1,10 @@
using System;
namespace ex_041_001_ConnectionStrings
{
class Nounours
{
public int Id { get; set; }
public string Nom { get; set; }
}
}

@ -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();
}
}
}
}

@ -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

@ -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> Nounours { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source=Nounours.db");
}
}

@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
namespace ex_041_001_ConnectionStrings
{
class SqlServerContext : DbContext
{
public DbSet<Nounours> Nounours { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=ex_041_001_ConnectionStrings.Nounours.mdf;Trusted_Connection=True;");
}
}

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

@ -1,8 +1,14 @@
#### to do #### to do
##### fundamentals (41) ##### fundamentals (41)
* connection strings * connection strings
* connection resiliency * exemple avec une connection string dans OnConfiguring
* testing => sqlite, inmemory * 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 ? * configuring a dbcontext ?
* nullable reference types => navigating (include, theninclude) * nullable reference types => navigating (include, theninclude)
##### creating a model (42) ##### creating a model (42)

Loading…
Cancel
Save