Skip to content

Unique constraints in EF Core: retarget net10.0, EF Core 10, built-in OpenAPI - #2209

Merged
vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/109680-efcore-add-unique-constraints-to-a-property-code-first
Sep 18, 2026
Merged

vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/109680-efcore-add-unique-constraints-to-a-property-code-first

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Unique constraints in EF Core: retarget to net10.0 and modernise the sample.

Both projects move from net8.0 to net10.0.

Package versions re-queried from the NuGet flat-container index on 2026-09-15 and shipped as read that day:

Package From To
Microsoft.EntityFrameworkCore 8.0.2 10.0.12
Microsoft.EntityFrameworkCore.SqlServer 8.0.2 10.0.12
Microsoft.EntityFrameworkCore.InMemory 8.0.2 removed
Microsoft.AspNetCore.OpenApi 8.0.2 10.0.12
Swashbuckle.AspNetCore 6.4.0 removed
Microsoft.AspNetCore.Mvc.Testing 8.0.2 10.0.12
Microsoft.NET.Test.Sdk 17.8.0 18.10.1
xunit 2.5.3 2.9.3
xunit.runner.visualstudio 2.5.3 4.0.0
coverlet.collector 6.0.0 10.0.1
Testcontainers.MsSql 3.7.0 4.15.0
FluentAssertions 6.12.0 7.2.2

FluentAssertions stays on the 7.x line, which is still Apache-2.0. 7.2.2 needed no assertion edit here, so the fallback pin to 6.12.2 was not taken.

InMemory is removed because no source file in either project references it. Swashbuckle is replaced by the built-in document: AddOpenApi() and MapOpenApi().

Code changes:

  • Planet gains OrbitalPeriod, which the article's composite index examples use and the model did not have.
  • Add() replaces AddAsync(). The async overload exists for value generators that need database access; the docs say the non-async method should be used for every other case, and this model uses an identity key.
  • catch (DbUpdateException) replaces the bare catch, which turned a duplicate name, a validation failure and a dead connection into the same 400.
  • .WithOpenApi() is dropped. It is deprecated on .NET 10 and raises ASPDEPR002; the document is still generated by AddOpenApi() plus MapOpenApi().
  • MsSqlBuilder takes the image parameter. The parameterless constructor is obsolete on Testcontainers 4.x (CS0618), and the image is the one the article's docker run command already names.
  • The connection string reads Database=SolarSystem instead of Database=Master, so EnsureCreatedAsync stops creating application tables in the server's master database.
  • Both live tests set OrbitalPeriod, which is a required member.

Verification on this machine, SDK 10.0.302 with runtime 10.0.10: dotnet build -c Release gives 0 errors and 0 warnings.

Note on CI for this folder: the workflow runs the solution with --filter "FullyQualifiedName!~Live", and AddPlanetEndpointLiveTests is the only test class here, so CI runs zero tests and a green check proves the build only. Test discovery was checked separately so a silent zero is not mistaken for a pass: dotnet test --list-tests reports both tests on xunit 2.9.3 with runner 4.0.0. The two tests themselves need Docker and have not been executed on this machine; they are owed a run on a machine with Docker before this merges.

… OpenAPI

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.
@vladimir-pecanac-main
vladimir-pecanac-main merged commit 02bf2c6 into CodeMazeBlog:main Sep 18, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant