Why does this code have a Required attribute?
It uses the same
System.ComponentModel.DataAnnotations
as the Models in the folder above it, but this isn't a Entity Framework Model
https://github.com/PacktPublishing/ASP.NET-Core-6-and-Angular/blob/main/Chapter_13/WorldCities/WorldCitiesAPI/Data/LoginRequest.csGitHub
ASP.NET-Core-6-and-Angular/LoginRequest.cs at main · PacktPublishin...
ASP.NET-Core-6-and-Angular, published by Packt. Contribute to PacktPublishing/ASP.NET-Core-6-and-Angular development by creating an account on GitHub.
3 Replies
The
System.ComponentModel.DataAnnotations
attributes are also used for validating models on client side while at the same time Entity framework uses them for modeling the database, for example if a property has a Required attribute it generates it as a non nullable column. Its done like this so you can reuse same models and same validation logic on client and server.
Its important to note that due to database software limitations Entity Framework can't validate all data annotations on the server hence some of them are only validated on the client side.
You can check this link for more information on some of the data annotations that Entity Framework supports: https://learn.microsoft.com/en-us/ef/core/modeling/entity-properties?tabs=data-annotations
And this link contains all data annotations and short descriptions about them:
https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations?view=net-7.0
Although lately sharing a single model as a data entity and an input model is less frequently done.
Oh and these are some extra attributes that are used by Entity Framework:
https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.schema?view=net-7.0@Pendramon thank you! I just downloaded postman to confirm and it does client validation
thanks. i'm taking a break and will look at the links more closely