Back to: ASP.NET Core Tutorials For Beginners and Professionals
MaxLength and MinLength Attribute in Entity Framework Core
In this article, I am going to discuss MaxLength and MinLength Data Annotation Attributes in Entity Framework Core (EF Core) with Examples. Please read our previous article, where we discussed Required Attribute in Entity Framework Core with Examples.
MaxLength and MinLength Attribute in Entity Framework Core
The MaxLength Data Annotation Attribute in Entity Framework Core specifies the maximum length of data that can be allowed for a property. This MaxLength Attribute will set the corresponding table column size in the database. In simple words, we can say that using MaxLength Data Annotation Attribute, we can set the size of the database column in the database. If we enter a value greater than the specified size, it will throw an exception.
The MinLength Data Annotation Attribute in Entity Framework Core specifies the minimum length of data that can be allowed for a property. In this case, it will set the size of the corresponding database table column in the database as max. In simple words, we can say that using MinLength Data Annotation Attribute, we can validate the data length we will store in the property. If we enter a value less than the specified size, it will throw an exception. It is a validation attribute that will not change the database schema.
By default, for string or byte[] properties of an entity, Entity Framework will set the size of the database column as max. For string properties, it will create the column as nvarchar(max), and for byte[] properties, it will create the column as varbinary(max).
Example to Understand MaxLength and MinLength Attribute in EF Core
Let us understand the default convention with an example, and then we will see how to use MinLength and MaxLength Data Annotation Attributes in EF Core. Please modify the Student Entity as follows. Here, you can see we have created the Student Entity with four properties. One Integer Property, two string properties, and one byte[] property. In this case, for string properties, it will set the corresponding database column as nvarchar(max). For the byte[] property, it will set the corresponding database column as varbinary(max).
namespace EFCoreCodeFirstDemo.Entities { public class Student { public int StudentId { get; set; } public string? FirstName { get; set; } public string? LastName { get; set; } public byte[]? Photo { get; set; } } }
Next, modify the context class as shown below. As you can see here, we have registered the Student model within the context class using DbSet.
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 above 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 DBMig1. The name that you are giving it should not be given earlier.
Now, if you check the Students database table, you will see it created the corresponding database columns with the maximum size shown in the image below.
How to Set the Max Length and Min Length in EF Core?
Now, we need to provide some restrictions on the data that we are going to store in the database. Our requirement is the maximum length for the First Name value is 50 Characters. The Minimum Length for the Last Name value is 5 Characters. We need to use the MaxLength(50) and MinLength(5) Attributes for this.
So, modify the Student Entity Class as follows. Here, you can see we have applied the MaxLength(50) Data Annotation Attribute on the FirstName Property, which will also set the corresponding database column length as 50. If we enter the FirstName value of more than 50 characters, it will throw an exception. Then we applied the MinLength(5) Data Annotation Attribute with the LastName Property, and this will throw an exception if we enter a value of less than 5 characters. For MinLength Attribute, Entity Framework will set the corresponding database column length as max.
using System.ComponentModel.DataAnnotations; namespace EFCoreCodeFirstDemo.Entities { public class Student { public int StudentId { get; set; } [MaxLength(50)] public string? FirstName { get; set; } [MinLength(5)] public string? LastName { get; set; } } }
With the above 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 DBMig2. The name that you are giving it should not be given earlier.
If you verify the database, you will see that the FirstName column will be created with size 50, and the LastName column will be created with the size max, as shown in the image below.
Note: You need to remember that Entity Framework validates the MaxLength and MinLength property values. For MaxLength, whatever value you specified will be set as the corresponding database column size. And For MinLength, Entity Framework will set the corresponding database column size as max. Th MinLength validation will come into the picture when we work with MVC Application.
How can we set both MaxLength and MinLength Attribute in a single Property?
Applying both MaxLength and MinLength Attribute in a single Property is also possible. For example, our requirement is to set the Maximum Length of the Student’s first name as 10 Characters and the Minimum Length for the Student’s first name as 5 Characters. Then we need to use both MaxLength and MinLength Attribute in the FirstName property of the Student Entity as follows.
using System.ComponentModel.DataAnnotations; namespace EFCoreCodeFirstDemo.Entities { public class Student { public int StudentId { get; set; } [MaxLength(10), MinLength(5)] public string? FirstName { get; set; } public string? LastName { get; set; } } }
In the next article, I am going to discuss DatabaseGenerated Attribute in Entity Framework Core with Examples. In this article, I try to explain the MaxLength and MinLength Data Annotation Attributes in Entity Framework Core with Examples. I hope you enjoyed this article’s MaxLength and MinLength Attribute in EF Core with Examples.