Repository navigation
Migration from v10 to v11
This guide covers breaking changes and new features when migrating from v10 to v11.
-
Dropped support for .NET 8 - Minimum target framework is now
net9.0 -
Removed the package
Thinktecture.Runtime.Extensions.EntityFrameworkCore8- UseEntityFrameworkCore9,EntityFrameworkCore10orEntityFrameworkCore11 -
The package
Thinktecture.Runtime.Extensions.EntityFrameworkCore9targetsnet9.0- In version 10 it targetsnet8.0 - Minimum C# version increased to C# 13, minimum .NET SDK increased to the .NET 10 SDK
See Target framework and packages for the details.
-
Regular unions are
closedin projects that compile as C# 15 - the source generator declares the union typeclosedinstead ofabstract -
Switch/Mapof a nested regular union -SwitchandMapof the outer union have no parameter for a nested type that carries[RegularUnion];SwitchPartiallyandMapPartiallykeep it
See Regular unions are closed with C# 15 and Switch and Map of nested regular unions for the details.
-
Ad hoc unions are C# unions in projects that compile as C# 15 - the source generator adds
[System.Runtime.CompilerServices.Union]and the membersHasValueandTryGetValue -
Configurations that do not fit a C# union are error TTRESG086 - change the configuration or set
SkipCSharpUnion = true -
Patterns test the content -
union is nullon a class union, andn is nullon aNullable<T>of a struct union, can change their result - New implicit conversions and System.Text.Json support of .NET 11 - overload resolution and serialization without the Thinktecture converter can change
See Ad hoc unions are C# unions with C# 15 for the details.
The packages of version 11 target net9.0. This includes the package Thinktecture.Runtime.Extensions.EntityFrameworkCore9, which targets net8.0 in version 10. The package Thinktecture.Runtime.Extensions.EntityFrameworkCore10 targets net10.0, and the package Thinktecture.Runtime.Extensions.EntityFrameworkCore11 targets net11.0, as in version 10. A project with an older target framework fails at restore:
error NU1202: Package Thinktecture.Runtime.Extensions 11.0.0 is not compatible with net8.0 (.NETCoreApp,Version=v8.0). Package Thinktecture.Runtime.Extensions 11.0.0 supports: net9.0 (.NETCoreApp,Version=v9.0)
Such a project stays on version 10 or moves to net9.0 or later. The documentation of version 10 is archived under Version 10.
A project with several target frameworks fails at restore as a whole when one of them is older than net9.0, for example <TargetFrameworks>net8.0;net10.0</TargetFrameworks>. Remove the old target frameworks, or stay on version 10 with all packages.
| Entity Framework Core | Package | Target framework |
|---|---|---|
| 9 | Thinktecture.Runtime.Extensions.EntityFrameworkCore9 |
net9.0 |
| 10 | Thinktecture.Runtime.Extensions.EntityFrameworkCore10 |
net10.0 |
| 11 | Thinktecture.Runtime.Extensions.EntityFrameworkCore11 |
net11.0 |
The package EntityFrameworkCore8 has no version 11. A project on Entity Framework Core 8 stays on version 10 with all packages, or moves to Entity Framework Core 9, 10 or 11.
An update of all packages of such a project leaves EntityFrameworkCore8 at version 10 and moves the other packages to version 11. In a project that targets net9.0 or later, NuGet restores this combination without a message. It is not supported. Keep all packages at version 10 until the project moves to Entity Framework Core 9, 10 or 11.
A project that targets net9.0 uses EntityFrameworkCore9, because Entity Framework Core 10 needs net10.0 and Entity Framework Core 11 needs net11.0. A project that targets net8.0 and uses Entity Framework Core 9 stays on version 10 with all packages, or moves to net9.0 or later.
All Thinktecture.Runtime.Extensions.* packages of a project must have the same version. This includes Thinktecture.Runtime.Extensions.SourceGenerator, Thinktecture.Runtime.Extensions.Analyzers and Thinktecture.Runtime.Extensions.Refactorings when a project references them directly.
- C# 13 (or higher) for generated code. C# 13 is the default language version of
net9.0. - .NET 10 SDK (or higher) for building projects.
The generated code contains no conditional compilation for target frameworks any more. For a project that targets net9.0 or later the compiled code is the same as in version 10, with these exceptions:
-
SwitchandMapof a union with a nested regular union (see below). - A regular union in a project that compiles as C# 15 is
closed(see below). - An ad hoc union in a project that compiles as C# 15 has the attribute
System.Runtime.CompilerServices.UnionAttributeand the membersHasValueandTryGetValue(see below).
The source generator declares a regular union type as closed when the project compiles as C# 15 or later and the type System.Runtime.CompilerServices.IsClosedTypeAttribute is available. Both conditions hold for a project that targets net11.0. A project that targets net9.0 or net10.0 compiles as C# 13 or C# 14 by default, and this section does not apply to it.
What changes for a closed union:
- A
switchexpression over the union needs no discard arm. A missing case is warning CS8509. - An arm that catches the rest by type after all cases is an error, because the compiler knows that nothing is left:
AnimalRecord other => ...,{ } other => ...andnot null => ...are error CS8510, andcase AnimalRecord:in aswitchstatement is error CS8120. Remove the arm. This applies to projects that reference a library with such a union as well, when they compile as C# 15. - Patterns that the compiler can now decide give new warnings:
x is Dog or Cat or null(CS8794),x is not Dog and not Cat and not null(CS8518), and adefaultlabel after all cases andcase null(CS0162). - A type in another assembly cannot derive from the union type (error CS9382). Version 10 allowed this for a record union through the copy constructor, although
SwitchandMapdid not know such a type. - A nested case of a generic union must derive from the union with the same type arguments (error CS9383 for
class IntCase : Result<int>insideResult<T>). -
JsonSerializerOptions.InferClosedTypePolymorphismapplies to the union. A union type with a[JsonConverter], your own or the one of an object factory for System.Text.Json, fails with this option; add[JsonPolymorphic(InferClosedTypePolymorphism = false)]to the union type.
A union type that is declared abstract is not closed. The analyzer reports warning TTRESG111 on the modifier, and the code fix "Replace 'abstract' with 'closed'" changes it. Do not apply the fix in a project with a target framework that compiles as C# 14 or older: keep abstract there and suppress TTRESG111, or remove the modifier. To keep a union as it was in version 10, keep abstract and suppress TTRESG111.
A JsonSerializerContext (System.Text.Json source generation) needs a modifier that you write yourself, closed or abstract, on the union type. It does not see the modifier of the generated part.
In a project with several target frameworks the union is closed only in the builds that fulfil both conditions. Code that all targets share uses Switch/Map, or a switch expression with a discard arm.
1. Condition. The source generator adds the attribute System.Runtime.CompilerServices.UnionAttribute to an ad hoc union when the project compiles as C# 15 or later and the attribute type is available. Both conditions hold for a project that targets net11.0. A project that targets net9.0 or net10.0 compiles as C# 13 or C# 14 by default, and nothing changes for it. See Using an ad hoc union as a C# union.
2. Configurations that do not fit. A union whose configuration does not fit a C# union gets no attribute, and the source generator reports error TTRESG086. These configurations are: a non-public ConstructorAccessModifier, a renamed or non-public Value property, SingleBackingFieldType, ConversionFromValue = None or Explicit, a duplicated member type, a stateless member that is a reference type, a Nullable<T> or a type parameter without a struct constraint, a public constructor of your own with one parameter that is passed by value, with in, or as params, your own public HasValue or TryGetValue, and the nested interface IUnionMembers. The code fix "Set 'SkipCSharpUnion = true'" supports "Fix all", so one step changes all affected unions. The light bulb on the union attribute offers the same action as a refactoring, also in an IDE that shows no code fix for a diagnostic of a source generator. In dotnet build, TTRESG086 hides the analyzer diagnostics of the same build, so other analyzer findings appear after the TTRESG086 errors are fixed. With SkipCSharpUnion = true the type stays as in version 10. <NoWarn>, .editorconfig and #pragma warning disable have no effect on TTRESG086. Never suppress TTRESG086 with [SuppressMessage]: the compiler then writes no new assembly. See Configurations that do not fit a C# union (TTRESG085, TTRESG086).
3. is null tests the content. On a class union that is a C# union, union is null, union is not null, case null and union is object test the content and not the reference. The result changes only when a member can hold null, for example a member with T1IsNullableReferenceType = true. The same applies to n is null when n is a Nullable<T> of a struct union: it is also true when n has a value whose content is null. No diagnostic reports this change. A type pattern changes as well: in version 10, union is int on an ad hoc union gave warning CS0184 and was always false; on a C# union it tests the content and can be true. Search the code that uses such a union for is null, is not null, case null and is object, and replace a reference check with ReferenceEquals(union, null), union == null or union is { }, and a check of a Nullable<T> with n.HasValue. This also applies to your own code inside the partial union type.
4. Patterns of the union's own type. union is TUnion x, union is not TUnion and case TUnion x: are error CS8121 for a C# union. This includes if (n is TUnion u) when n is a Nullable<T> of a struct union. Write union is { } x or n is { } u instead.
5. New implicit conversions. The compiler adds an implicit conversion from every member type that has a public constructor. This includes an interface member, an object member and a type parameter member, for which the library generates no conversion operator. A call with overloads for object and for the union can therefore choose the union overload, and a call can become ambiguous (error CS0121) when the union has an object member. See Conversions.
6. System.Text.Json of .NET 11. System.Text.Json of .NET 11 serializes a C# union on its own when no Thinktecture JSON converter is in use. Version 10 threw InvalidOperationException in this case. The JSON contains the value of the member without a discriminator, and Validate is not called. With an object factory for System.Text.Json and the Thinktecture JSON converter, the JSON format stays the same. See Serialization and Entity Framework Core.
7. New public members. A C# union has the public members HasValue and TryGetValue. They carry [EditorBrowsable(EditorBrowsableState.Never)], because they exist for the compiler. In your own code, use IsX/AsX or Switch/Map.
8. Libraries and projects with several target frameworks. A consumer that compiles as C# 14 or older cannot write is null, is not null or a type pattern on a C# union of a library (error CS9327, or CS8652 with the .NET 10 SDK). A library with public ad hoc unions for such consumers sets SkipCSharpUnion = true on these unions. In a project with several target frameworks, only the builds that fulfil the condition of point 1 get C# unions. Code that all targets share uses Switch/Map, ReferenceEquals(union, null) or union is { }.
9. A written attribute keeps working. An ad hoc union with [System.Runtime.CompilerServices.Union] that you wrote yourself keeps the rules of version 10.7, and the source generator adds no second attribute. The analyzer reports TTRESG085 for a configuration that does not fit, and for the attribute together with SkipCSharpUnion = true. The source generator reports warning TTRESG112 for the configurations with different semantics.
A nested type with [RegularUnion] that is declared without abstract had its own parameter in the Switch and Map methods of the outer union, for example failure next to failureNotFound and failureUnauthorized. This handler could never run, because every instance is one of the cases of the nested union. The parameter is removed from Switch and Map. Remove the argument at the call sites.
SwitchPartially and MapPartially keep the parameter. There it is the fallback for the cases of the nested union that have no handler of their own.
A Map overload of [UnionSwitchMapOverload] is not generated when an earlier Map overload has the same number of parameters.