Data Annotations Required Attribute EF 6 & EF Core

The Required attribute is used for generating NOT NULL columns. EF 6 & EF Core will create a NOT NULL column if it detects a property having the Requiredattribute.

Data Annnotation Required Attribute in Entity Framework Code First
Data Annotation Required Attribute in Entity Framework Code First

Best Entity Framework Core Books

Best Entity Framework Books

In the above example, the Name property is decorated with Required Attribute. This will create the Name column as Not Null in the database. Without the Required attribute, all string properties are mapped to NULLABLE columns ( Example Address in the above model)

ASP.NET MVC also uses this attribute to Validate the model in the User interface

References

  1. RequiredAttribute Class

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top