Skip to content

Commit 02bf2c6

Browse files
Unique constraints in EF Core: retarget net10.0, EF Core 10, built-in OpenAPI (#2209)
Retarget both projects from net8.0 to net10.0. Packages: EF Core and EF Core SqlServer 8.0.2 to 10.0.12, AspNetCore.OpenApi 8.0.2 to 10.0.12, Mvc.Testing 8.0.2 to 10.0.12, Test.Sdk 17.8.0 to 18.10.1, xunit 2.5.3 to 2.9.3, xunit.runner.visualstudio 2.5.3 to 4.0.0, coverlet 6.0.0 to 10.0.1, Testcontainers.MsSql 3.7.0 to 4.15.0, FluentAssertions 6.12.0 to 7.2.2. Removed Swashbuckle.AspNetCore (replaced by AddOpenApi and MapOpenApi) and EntityFrameworkCore.InMemory (referenced by no source file). Code: - Planet gains OrbitalPeriod, which the article's composite index examples use. - Program.cs: AddOpenApi/MapOpenApi in place of Swagger; Add instead of AddAsync (the async form exists for value generators that hit the database); catch (DbUpdateException) instead of a bare catch, which is the exception a unique index violation arrives as; .WithOpenApi() dropped, deprecated on .NET 10 (ASPDEPR002). - SolarSystemApiApplicationFactory: MsSqlBuilder takes the image parameter, the parameterless constructor is obsolete on Testcontainers 4.x (CS0618). - Connection string: Database=SolarSystem instead of Database=Master, so the sample stops creating its tables in the server's master database. - Both live tests set OrbitalPeriod, now a required member. Build: 0 errors, 0 warnings on SDK 10.0.302. Repo CI runs no tests in this folder, its filter excludes every Live-named test.
1 parent f015a50 commit 02bf2c6

7 files changed

Lines changed: 28 additions & 30 deletions

File tree

‎dotnet-efcore/UniqueConstraintsInEFCore/UniqueConstraintsInEFCore.Tests/AddPlanetEndpointLiveTests.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public async Task GivenThatPlanetDoesNotExist_WhenAddPlanetEndpointIsInvoked_The
1212
{
1313
Name = "Mars",
1414
Mass = 6.4171,
15-
Radius = 3389.5
15+
Radius = 3389.5,
16+
OrbitalPeriod = 686.98
1617
};
1718

1819
// Act
@@ -30,8 +31,9 @@ public async Task GivenThatPlanetExist_WhenAddPlanetEndpointIsInvoked_Then400IsR
3031
var planet = new Planet
3132
{
3233
Name = "Venus",
33-
Mass = 6.4171,
34-
Radius = 3389.5
34+
Mass = 4.8675,
35+
Radius = 6051.8,
36+
OrbitalPeriod = 224.7
3537
};
3638

3739
// Act

‎dotnet-efcore/UniqueConstraintsInEFCore/UniqueConstraintsInEFCore.Tests/SolarSystemApiApplicationFactory.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
namespace UniqueConstraintsInEFCore.Tests;
1+
namespace UniqueConstraintsInEFCore.Tests;
22

33
public class SolarSystemApiApplicationFactory : WebApplicationFactory<Program>, IAsyncLifetime
44
{
5-
private readonly MsSqlContainer _msSqlContainer = new MsSqlBuilder().Build();
5+
private readonly MsSqlContainer _msSqlContainer = new MsSqlBuilder("mcr.microsoft.com/mssql/server:2022-latest").Build();
66

77
protected override void ConfigureWebHost(IWebHostBuilder builder)
88
{

‎dotnet-efcore/UniqueConstraintsInEFCore/UniqueConstraintsInEFCore.Tests/UniqueConstraintsInEFCore.Tests.csproj‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

@@ -10,13 +10,13 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14-
<PackageReference Include="FluentAssertions" Version="6.12.0" />
15-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.2" />
16-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
17-
<PackageReference Include="Testcontainers.MsSql" Version="3.7.0" />
18-
<PackageReference Include="xunit" Version="2.5.3" />
19-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
13+
<PackageReference Include="coverlet.collector" Version="10.0.1" />
14+
<PackageReference Include="FluentAssertions" Version="7.2.2" />
15+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.12" />
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
17+
<PackageReference Include="Testcontainers.MsSql" Version="4.15.0" />
18+
<PackageReference Include="xunit" Version="2.9.3" />
19+
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

‎dotnet-efcore/UniqueConstraintsInEFCore/UniqueConstraintsInEFCore/Data/Models/Planet.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public class Planet
99
public required string Name { get; set; }
1010
public required double Mass { get; set; }
1111
public required double Radius { get; set; }
12+
public required double OrbitalPeriod { get; set; }
1213
}

‎dotnet-efcore/UniqueConstraintsInEFCore/UniqueConstraintsInEFCore/Program.cs‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using Microsoft.EntityFrameworkCore;
1+
using Microsoft.EntityFrameworkCore;
22
using UniqueConstraintsInEFCore.Data;
33
using UniqueConstraintsInEFCore.Data.Models;
44
using UniqueConstraintsInEFCore.Services;
55

66
var builder = WebApplication.CreateBuilder(args);
77

8-
builder.Services.AddEndpointsApiExplorer();
9-
builder.Services.AddSwaggerGen();
8+
builder.Services.AddOpenApi();
109

1110
builder.Services.AddHostedService<InitializationService>();
1211

@@ -17,8 +16,7 @@
1716

1817
if (app.Environment.IsDevelopment())
1918
{
20-
app.UseSwagger();
21-
app.UseSwaggerUI();
19+
app.MapOpenApi();
2220
}
2321

2422
app.UseHttpsRedirection();
@@ -27,18 +25,17 @@
2725
{
2826
try
2927
{
30-
await context.Planets.AddAsync(planet);
28+
context.Planets.Add(planet);
3129
await context.SaveChangesAsync();
3230

3331
return Results.Created($"/planets/{planet.Id}", planet);
3432
}
35-
catch
33+
catch (DbUpdateException)
3634
{
3735
return Results.BadRequest();
3836
}
3937
})
40-
.WithName("AddPlanet")
41-
.WithOpenApi();
38+
.WithName("AddPlanet");
4239

4340
app.Run();
4441

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" />
11-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" />
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
14-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.12" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.12" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.12" />
1513
</ItemGroup>
1614

1715
</Project>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"Logging": {
33
"LogLevel": {
44
"Default": "Information",
@@ -7,6 +7,6 @@
77
},
88
"AllowedHosts": "*",
99
"ConnectionStrings": {
10-
"SolarSystemDatabase": "Server=127.0.0.1,1433; Database=Master ;User Id=SA; Password=yourStrong(!)Password; TrustServerCertificate=True"
10+
"SolarSystemDatabase": "Server=127.0.0.1,1433; Database=SolarSystem;User Id=SA; Password=yourStrong(!)Password; TrustServerCertificate=True"
1111
}
1212
}

0 commit comments

Comments
 (0)