Skip to content

Commit 5b59272

Browse files
committed
feat(csharp): add async / ef
1 parent f19e20d commit 5b59272

38 files changed

+1096
-2
lines changed

.config/dotnet-tools.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"dotnet-ef": {
6+
"version": "6.0.1",
7+
"commands": [
8+
"dotnet-ef"
9+
]
10+
}
11+
}
12+
}

Samples.sln

+43-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,25 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesignPatterns.Mediator.Cor
5252
EndProject
5353
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Singleton", "Singleton", "{E0676D1B-04DC-4D4E-80FA-402F27D6E622}"
5454
EndProject
55-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Singleton", "src\3. DesignPatterns\DesignPatterns.Singleton\DesignPatterns.Singleton.csproj", "{6DAC00EA-0CEC-4A0D-9647-F73135F060A2}"
55+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesignPatterns.Singleton", "src\3. DesignPatterns\DesignPatterns.Singleton\DesignPatterns.Singleton.csproj", "{6DAC00EA-0CEC-4A0D-9647-F73135F060A2}"
5656
EndProject
57-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Composite", "src\3. DesignPatterns\DesignPatterns.Composite\DesignPatterns.Composite.csproj", "{3278DC26-37E5-408C-BECE-4C1288A65D1F}"
57+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DesignPatterns.Composite", "src\3. DesignPatterns\DesignPatterns.Composite\DesignPatterns.Composite.csproj", "{3278DC26-37E5-408C-BECE-4C1288A65D1F}"
58+
EndProject
59+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "4 - Entity Framework Core", "4 - Entity Framework Core", "{A671DCDC-68BD-41F3-9384-E40B98FB270B}"
60+
EndProject
61+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "src\4. Entity Framework Core\ConsoleApp\ConsoleApp.csproj", "{E27FAB6F-AE92-44EB-B809-1FB9D1ECE23C}"
62+
EndProject
63+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Infrastructure", "src\4. Entity Framework Core\Infrastructure\Infrastructure.csproj", "{CA771970-8F9F-4C65-8FDC-7CEAB3A2448D}"
64+
EndProject
65+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "5 - Entity Framework Api", "5 - Entity Framework Api", "{1807AC57-8B02-4033-B40D-EF5F271C5C4A}"
66+
EndProject
67+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp", "src\5. Entity Framework Api\WebApp\WebApp.csproj", "{2ED8D7D1-993A-45F4-B9FD-70740809A96E}"
68+
EndProject
69+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "6 - Async", "6 - Async", "{DF30EDA4-4841-4293-B69F-3356935C6BB7}"
70+
EndProject
71+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinApp", "src\6. Async\WinApp\WinApp.csproj", "{9A0DF74D-4CBE-44EF-884C-62265B264407}"
72+
EndProject
73+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "src\6. Async\ConsoleApp\ConsoleApp.csproj", "{B788B805-DF8D-4B56-8B2E-1D4FDF75C770}"
5874
EndProject
5975
Global
6076
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -114,6 +130,26 @@ Global
114130
{3278DC26-37E5-408C-BECE-4C1288A65D1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
115131
{3278DC26-37E5-408C-BECE-4C1288A65D1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
116132
{3278DC26-37E5-408C-BECE-4C1288A65D1F}.Release|Any CPU.Build.0 = Release|Any CPU
133+
{E27FAB6F-AE92-44EB-B809-1FB9D1ECE23C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
134+
{E27FAB6F-AE92-44EB-B809-1FB9D1ECE23C}.Debug|Any CPU.Build.0 = Debug|Any CPU
135+
{E27FAB6F-AE92-44EB-B809-1FB9D1ECE23C}.Release|Any CPU.ActiveCfg = Release|Any CPU
136+
{E27FAB6F-AE92-44EB-B809-1FB9D1ECE23C}.Release|Any CPU.Build.0 = Release|Any CPU
137+
{CA771970-8F9F-4C65-8FDC-7CEAB3A2448D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
138+
{CA771970-8F9F-4C65-8FDC-7CEAB3A2448D}.Debug|Any CPU.Build.0 = Debug|Any CPU
139+
{CA771970-8F9F-4C65-8FDC-7CEAB3A2448D}.Release|Any CPU.ActiveCfg = Release|Any CPU
140+
{CA771970-8F9F-4C65-8FDC-7CEAB3A2448D}.Release|Any CPU.Build.0 = Release|Any CPU
141+
{2ED8D7D1-993A-45F4-B9FD-70740809A96E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
142+
{2ED8D7D1-993A-45F4-B9FD-70740809A96E}.Debug|Any CPU.Build.0 = Debug|Any CPU
143+
{2ED8D7D1-993A-45F4-B9FD-70740809A96E}.Release|Any CPU.ActiveCfg = Release|Any CPU
144+
{2ED8D7D1-993A-45F4-B9FD-70740809A96E}.Release|Any CPU.Build.0 = Release|Any CPU
145+
{9A0DF74D-4CBE-44EF-884C-62265B264407}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
146+
{9A0DF74D-4CBE-44EF-884C-62265B264407}.Debug|Any CPU.Build.0 = Debug|Any CPU
147+
{9A0DF74D-4CBE-44EF-884C-62265B264407}.Release|Any CPU.ActiveCfg = Release|Any CPU
148+
{9A0DF74D-4CBE-44EF-884C-62265B264407}.Release|Any CPU.Build.0 = Release|Any CPU
149+
{B788B805-DF8D-4B56-8B2E-1D4FDF75C770}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
150+
{B788B805-DF8D-4B56-8B2E-1D4FDF75C770}.Debug|Any CPU.Build.0 = Debug|Any CPU
151+
{B788B805-DF8D-4B56-8B2E-1D4FDF75C770}.Release|Any CPU.ActiveCfg = Release|Any CPU
152+
{B788B805-DF8D-4B56-8B2E-1D4FDF75C770}.Release|Any CPU.Build.0 = Release|Any CPU
117153
EndGlobalSection
118154
GlobalSection(SolutionProperties) = preSolution
119155
HideSolutionNode = FALSE
@@ -141,6 +177,11 @@ Global
141177
{E0676D1B-04DC-4D4E-80FA-402F27D6E622} = {E14BF56A-F26B-4698-91B4-E1931BDB4D56}
142178
{6DAC00EA-0CEC-4A0D-9647-F73135F060A2} = {E0676D1B-04DC-4D4E-80FA-402F27D6E622}
143179
{3278DC26-37E5-408C-BECE-4C1288A65D1F} = {E14BF56A-F26B-4698-91B4-E1931BDB4D56}
180+
{E27FAB6F-AE92-44EB-B809-1FB9D1ECE23C} = {A671DCDC-68BD-41F3-9384-E40B98FB270B}
181+
{CA771970-8F9F-4C65-8FDC-7CEAB3A2448D} = {A671DCDC-68BD-41F3-9384-E40B98FB270B}
182+
{2ED8D7D1-993A-45F4-B9FD-70740809A96E} = {1807AC57-8B02-4033-B40D-EF5F271C5C4A}
183+
{9A0DF74D-4CBE-44EF-884C-62265B264407} = {DF30EDA4-4841-4293-B69F-3356935C6BB7}
184+
{B788B805-DF8D-4B56-8B2E-1D4FDF75C770} = {DF30EDA4-4841-4293-B69F-3356935C6BB7}
144185
EndGlobalSection
145186
GlobalSection(ExtensibilityGlobals) = postSolution
146187
SolutionGuid = {7BB9A993-DEC9-434F-B537-287AE57B4536}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<AssemblyName>EfCore.ConsoleApp</AssemblyName>
9+
<RootNamespace>EfCore.ConsoleApp</RootNamespace>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using EfCore.Infrastructure;
2+
3+
InitializeDb();
4+
5+
RemoveClassRoom();
6+
7+
Console.WriteLine("Fin du programme");
8+
9+
Console.ReadLine();
10+
11+
void RemoveClassRoom()
12+
{
13+
using var dbContext = new ApplicationDbContext();
14+
15+
var classRoomToDelete = new Classroom(Guid.Parse("7b38297a-8341-4e75-9e37-1c44bebbc7e8"));
16+
dbContext.Classrooms.Remove(classRoomToDelete);
17+
18+
dbContext.SaveChanges();
19+
}
20+
21+
void InitializeDb()
22+
{
23+
using var dbContext = new ApplicationDbContext();
24+
25+
var classRoom1 = new Classroom(Guid.Parse("7b38297a-8341-4e75-9e37-1c44bebbc7e8"));
26+
var classRoom2 = new Classroom(Guid.Empty);
27+
28+
dbContext.Classrooms.Add(classRoom1);
29+
dbContext.Add(classRoom2);
30+
31+
dbContext.SaveChanges();
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace EfCore.Infrastructure;
4+
5+
public class ApplicationDbContext : DbContext
6+
{
7+
public DbSet<Classroom> Classrooms { get; init; }
8+
9+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
10+
{
11+
base.OnConfiguring(optionsBuilder);
12+
13+
optionsBuilder.UseInMemoryDatabase("MyApp");
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace EfCore.Infrastructure;
2+
3+
public class Classroom
4+
{
5+
public Classroom(Guid id): this(string.Empty, id)
6+
{
7+
}
8+
9+
public Classroom(string name, Guid id)
10+
{
11+
Name = name;
12+
}
13+
14+
public Guid Id { get; private set; }
15+
16+
public string Name { get; private set; }
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<AssemblyName>EfCore.Infrastructure</AssemblyName>
8+
<RootNamespace>EfCore.Infrastructure</RootNamespace>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.1" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using EfApi.WebApp.Data;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace WebApp.Controllers
6+
{
7+
[ApiController]
8+
[Route("[controller]")]
9+
public class WeatherController : ControllerBase
10+
{
11+
private readonly ILogger<WeatherController> _logger;
12+
private readonly ApplicationDbContext _dbContext;
13+
14+
public WeatherController(ILogger<WeatherController> logger, ApplicationDbContext dbContext)
15+
{
16+
_logger = logger;
17+
_dbContext = dbContext;
18+
}
19+
20+
[HttpGet()]
21+
public async Task<IEnumerable<Weather>> Get()
22+
{
23+
var w = await _dbContext.Weathers.Include(w => w.City).ToListAsync();
24+
25+
return w;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace EfApi.WebApp.Data
4+
{
5+
public class ApplicationDbContext : DbContext
6+
{
7+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
8+
{
9+
}
10+
11+
public DbSet<Weather> Weathers { get; init; }
12+
13+
public DbSet<City> Cities { get; init; }
14+
15+
protected override void OnModelCreating(ModelBuilder modelBuilder)
16+
{
17+
modelBuilder.Entity<Weather>().HasOne(w => w.City).WithMany().HasForeignKey(w => w.CityId);
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace EfApi.WebApp.Data
2+
{
3+
public class City
4+
{
5+
public City(Guid id, string name, string postalCode)
6+
{
7+
Id = id;
8+
Name = name;
9+
PostalCode = postalCode;
10+
}
11+
12+
public Guid Id { get; init; }
13+
14+
public string Name { get; set; }
15+
16+
public string PostalCode { get; init; }
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace EfApi.WebApp.Data
2+
{
3+
public class Weather
4+
{
5+
public Weather(Guid id, int value, City city): this(id, value, city.Id)
6+
{
7+
City = city;
8+
}
9+
10+
public Weather(Guid id, int value, Guid cityId)
11+
{
12+
Id = id;
13+
Value = value;
14+
CityId = cityId;
15+
}
16+
17+
public int Value { get; init; }
18+
19+
public Guid Id { get; init; }
20+
21+
public Guid CityId { get; init; }
22+
23+
public City? City { get; init; }
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace EfApi.WebApp.Dto
2+
{
3+
public class WeatherDto
4+
{
5+
public Guid Id { get; set; }
6+
7+
public int Value { get; set; }
8+
9+
public Guid CityId { get; set; }
10+
11+
public string CityName { get; set; }
12+
}
13+
}

src/5. Entity Framework Api/WebApp/Migrations/20220201082848_InitialDb.Designer.cs

+74
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)