The Required Attribute

The Required attribute is applied to an entity property to configure the mapped database column as not nullable.

By default, strings are mapped to nullable columns in the database, but this convention is overridden in the example below through the application of the Required attribute. The Title column in the Book table will be configured as not null:

public class Book
{
    public int BookId { get; set; }
    [Required]
    public string Title { get; set; }
    public Author Author { get; set; }
}

The Required attribute can be used to provide client-side validation in validation-aware UI frameworks, such as ASP.NET MVC.

Fluent API

The Fluent API equivalent for the Required attribute is the IsRequired method.


Created:
Last updated: 14/03/2017 16:33:43
Proficiency Level: Beginner