Put your product in front of .NET and C# developers.

New .NET 11 features for ASP.NET Core and EF Core

David Grace David Grace

.NET 11 has been released. You can learn what's new in .NET 11, or watch the video:

Download the code example

You'll need to fill out the code example form. We will send you an email where you can download the code example.

Software

This is the software that will need to be installed onto your machine.

Import the database

This application integrates with a SQL Server database. Inside the Database folder, there is RoundTheCode_DotNet11.bacpac and RoundTheCode_DotNet11.bak. Use either of these database backups to import the database to your local SQL Server database server.

The application

Inside Api/appsettings.Development.json, find ConnectionStrings:DotNet11DbContextand point the connection string to your imported database.

Open up RoundTheCode.DotNet11.slnx, and run the application. It will load on https://localhost:7058/swagger. Here is how you test each feature:

Short circuiting Controllers

ShortCircuit has been added to HealthController. In Program.cs, put a breakpoint inside app.Use and run /api/health. It will return 200 without the breakpoint triggering. The same will happen with /api/health/app-ready, but it will return a 204 response as ShortCircuit(204) has been added to that API endpoint.

Union tyes in Controllers

A union type has been added as the return type to Get in PaymentsController.

Run /api/v1/payments/1 and it will return CardPaymentDto. Run /api/v1/payments/2 and it will return BankTransferDto.

Union types in Minimal APIs

This works in the same way as Controllers, but it's been added into PaymentsEndpoints.

Run /api/v2/payments/1 and it will return CardPaymentDto. Run /api/v2/payments/2 and it will return BankTransferDto.

Async validation in Minimal APIs

A custom validation attribute has been added in UniqueEmailAttribute. This validates the Email property in RegisterEmailDto. This type is used as the request in the Register endpoint inside UsersEndpoints.

Run /api/v2/users/register (POST) with this body:

{
	"email": "[email protected]"
}

This will throw the validation that is set in UniqueEmailAttribute.

Async validation in Controllers not supported

RegisterEmailDto has been added to the Register method in UsersControllers.

Run /api/v1/users/register (POST) with this body:

{
	"email": "[email protected]"
}

A InvalidOperationException will be thrown stating that it can only be validated asynchronously.

Create and apply a migration in one step

If you have already imported the database, delete it. Also delete the Migrations folder in Infrastructure.

In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Console. Make sure that Infrastructure is set as the Default project.

Run:

Update-Database -Migration AddProducts -Add

This will add a product migration, create the database and apply products to that database.

If you use the .NET CLI, you can run this command:

dotnet ef database update AddProducts --add

MinBy and MaxBy support

These examples have been added to ProductRepository.

Run /api/v2/products/lowest-price and /api/v2/products/highest-price to test these. You must make sure that the database has been imported, and the connection string has been set up correctly.

Download this code example

Enter your email and we'll send you a download link.

Download this code example

Enter your email and we'll send you a download link.