Blog Logo
TAGS

New Data Annotation attributes with .NET 8

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. 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. The Range attribute has been extended so that it is now possible to specify whether the range values are exclusive. The Length attribute has been extended so that the maximum length can now also be specified. The new Base64String attribute ensures that the content is a compliant Base64 string. The AllowedValues attribute specifies which values are allowed for a string. The DeniedValues attribute prevents the specified values. 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.