Back to: ASP.NET Core Tutorials For Beginners and Professionals
Required Attribute in Entity Framework Core
In this article, I am going to discuss the Required Data Annotation Attribute in Entity Framework Core (EF Core) with Examples. Please read our previous article, where we discussed NotMapped Attribute in Entity Framework Core with Examples.
Required Attribute in Entity Framework Core
The Required Data Annotation Attribute in Entity Framework Core can be applied to one or more properties of an entity class. If we apply the Required Attribute to a Property, then Entity Framework will create a NOT NULL column for that Property in the database. NOT NULL Column means it will not accept NULL Value in that column in the database.
Now, if you go to the definition of Required Data Annotation Attribute, then you will see the following signature. The Required Attribute belongs to System.ComponentModel.DataAnnotations namespace.
The above Required Attribute class specifies that a data field value is required. As you can see, this class has one parameterless constructor, one property i.e. AllowEmptyStrings, and one overridden method, i.e., IsValid.
Examples to Understand Required Attribute in Entity Framework Core:
By default, for Nullable .NET Data types such as String, EF Core creates the column as a NULL column which can accept NULL Values. Let us first understand this default Entity Framework Core Conventions, and then we will see how to use the Required Data Annotation Attribute. First, modify the Student.cs class file as follows.
namespace EFCoreCodeFirstDemo.Entities { public class Student { public int StudentId { get; set; } public string Name { get; set; } public string Address { get; set; } public int RollNumber { get; set; } } }
As you can see, here we have created the Student Entity class with four properties. Two Integer properties and two string properties. As the integers are primitive types, they cannot hold null values by default. so, for the integer properties, the Entity Framework Core will create the database column with NOT NULL Constraint, which cannot store a null value. The string is a reference type, and as they hold a null value by default, so, for string type properties, the EF Core is going to create a NULL type column in the database that can store a NULL value.
Next, modify the context class as follows. As you can see here, we have registered the Student model class within the context class using the DbSet property.
using Microsoft.EntityFrameworkCore; namespace EFCoreCodeFirstDemo.Entities { public class EFCoreDbContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=LAPTOP-6P5NK25R\SQLSERVER2022DEV;Database=EFCoreDB;Trusted_Connection=True;TrustServerCertificate=True;"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { } public DbSet<Student> Students { get; set; } } }
With the changes in place, open Package Manager Console and Execute the following add-migration and update-database commands. You can give any name to your migration. Here, I am giving DBMigration101. The name that you are giving it should not be given earlier.
Now, if you verify the database, you will see NOT NULL columns for integer properties, and for string properties, you will see NULL columns, as shown in the image below.
How to make the Name Column a NOT NULL Column?
Now, what is our requirement, we need to accept a NULL value for the Address column, but we do not want to accept NULL Value for the Name column. To do so, we need to decorate the Required Data Annotation Attribute with the Name Property of our Student Entity Class. So, modify the Student.cs class file as follows.
using System.ComponentModel.DataAnnotations; namespace EFCoreCodeFirstDemo.Entities { public class Student { public int StudentId { get; set; } [Required] public string Name { get; set; } public string Address { get; set; } public int RollNumber { get; set; } } }
With the changes in place, open Package Manager Console and Execute the following add-migration and update-database commands. You can give any name to your migration. Here, I am giving DBMigration101. The name that you are giving it should not be given earlier.
Now, if you check the Students database table, you will see that the Name database column is created using the NOT NULL constraint, as shown in the image below.
How to allow empty strings in NOT NULL Properties?
Now, our requirement is we will not allow a NULL value, but we want to allow an Empty value. It would be best to remember that NULL and Empty are different. They are different. The value null represents the absence of any object, while the empty string is an object of type String with zero characters.
In order to accept an empty string in a NOT NULL string column, we need to use the AllowEmptyStrings property and set its value to true. This property of the Required Attribute class gets or sets a value indicating whether an empty string is allowed. You need to set the value to true if an empty string is allowed; otherwise, false. The default value is false.
Let us modify the Student Entity class as follows to use Required Attribute with AllowEmptyStrings property and let us set its value to true.
using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; namespace EFCoreCodeFirstDemo.Entities { public class Student { public int StudentId { get; set; } [Required(AllowEmptyStrings = true)] public string Name { get; set; } public string Address { get; set; } public int RollNumber { get; set; } } }
In the next article, I am going to discuss MaxLength and MinLength Attribute in Entity Framework Core with Examples. In this article, I try to explain Required Data Annotation Attribute in Entity Framework Core with Examples. I hope you enjoyed this Required Attribute in EF Core with Examples article.