Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

New Data Annotation attributes with .NET 8

New Data Annotation attributes with .NET 8
November 19, 2023 2 min read BEN ABT
EF Core .NET Csharp

This article was written in 2023. Versions, tools and recommendations may have changed since then. Newer posts are collected under EF Core.

With .NET 8, the System.ComponentModel.DataAnnotations Namespace has been revised and some new attributes have been added that have been requested by the community for some time and have only been implemented through custom implementations.

Required

The Required attribute has been revised and has new parameters. In addition to the normal mandatory validation, it is now also possible to exclude certain default values.

This is particularly useful for IDs that are simply mandatory, but it was still possible to set the struct default and Required was therefore virtually functionless for non-nullable props.

C#
1[Required(DisallowAllDefaultValues = true)]
2public Guid MyId { get; set; }

However, the better option in this case is still implementation using Strong-Ids .

Range

The Range attribute has been extended so that it is now possible to specify whether the range values are exclusive, which simplifies declarations.

C#
1[Range(0, 100, MinimumIsExclusive = true, MaximumIsExclusive = true)]
2public int Age { get; set; }

This means that the permitted range is now 1-99.

Length

The Length attribute has been extended so that the maximum length can now also be specified.

C#
1[Length(10, 20)]
2public string Username { get; set; }

This means that the permitted length will now be a minimum of 10 characters and a maximum of 20 characters.

Base64String

The new Base64String attribute ensures that the content is a compliant Base64 string.

C#
1[Base64String]
2public string Hash { get; set; }

This is particularly useful for validations, but should not be used for passwords - after all, passwords should always be stored in a one-way hash method including mechanisms such as salting - but never as Base64.

AllowedValues

AllowedValues is a new attribute that specifies which values are allowed for a string, for example

C#
1[AllowedValues("Batman", "Catwoman", "James Bond")]
2public string Hero { get; set; }

DeniedValues

DeniedValues is the counterpart to AllowedValues and prevents the specified values

C#
1[DeniedValues("Admin", "Administrator")]
2public string Username { get; set; }

Source Code Generator with Options

Data annotations are primarily known when used in the Entity Framework, but they can be used in many places, for example in Options .

In the case of options, data annotations are now inserted into the class via Source Code Generator , which speeds up validation overall and therefore also benefits the startup time of applications.

Related articles

Advanced Usage of MSSQL Indexed Views in EF Core

Jan 13, 2025 - 5 min read

Advanced Usage of MSSQL Indexed Views in EF Core

In the world of Microsoft SQL Server , views are a powerful tool for simplifying complex data queries. Simple views offer no real …

Increase Command Timeout for EF Core Migration Bundles

Jan 3, 2025 - 2 min read

Increase Command Timeout for EF Core Migration Bundles

EF Core Migration Bundles are standalone executable files that contain one or more Entity Framework Core migrations and can be applied …

The well-structured Entity in EF Core

Nov 16, 2023 - 7 min read

The well-structured Entity in EF Core

A database entity represents an identifiable entry, often also things from the real world, which are to be stored and managed in the …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026
Problem Details in ASP.NET Core and .NET 11

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026
Resilience in .NET with Microsoft.Extensions.Resilience

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026
What Matters in ASP.NET Core 11

What Matters in ASP.NET Core 11

Sep 14, 2026

Tags

.NET (192) Csharp (96) ASP.NET Core (48) Azure (41) DevOps (17) Docker (14) Microsoft (14) EF Core (11) Microsoft MVP (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Security (9) Synology (9) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7) Bicep (6)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact