Entity Framework Required Data Annotation
The Required
attribute can be applied to a property to make it a required property in an entity class.
- EF will create a
NOT NULL
column in a database table for a property on which theRequired
attribute is applied. - If you save an entity without assigning a value to the
Required
property, then EF 6 will throw theSystem.Data.Entity.Validation.DbEntityValidationException
exception.
public class Book { public int BookId { get; set; } [Required] public string Title { get; set; } }
ZZZ Projects