Back to: Entity Framework Tutorials For Begineers and Professionals
MaxLength and MinLength Attribute in Entity Framework
In this article, I am going to discuss MaxLength and MinLength Data Annotation Attributes in Entity Framework Code First Approach with Examples. Please read our previous article where we discussed Required Attribute in Entity Framework Code First Approach with Examples.
MaxLength and MinLength Attribute in Entity Framework
The MaxLength Data Annotation Attribute in Entity Framework specifies the maximum length of data value that can be allowed for a property. This MaxLength Attribute will set the size of the corresponding database table column 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 specifies the minimum length of data value 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 provide a validation of the length of the data that we are going to store in the property. If we enter a value less than the specified size, it will throw an exception. It is a validation attribute and it 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 MaxLength and MinLength Attribute in Entity Framework
Let us understand the default convention with an example and then we will see how to use MinLength and MaxLength Data Annotation Attributes. 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 EFCodeFirstDemo { public class Student { public int StudentId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public byte[] Photo { get; set; } } }
As we are going to update the Student Entity Model many times, in order to avoid the Run-Time Exception when the model changes and when we rerun the application, let us set the database initializer as DropCreateDatabaseIfModelChanges. So, modify the context class as follows. As you can see here, we have registered the Student model within the context class using DbSet.
using System.Data.Entity; namespace EFCodeFirstDemo { public class EFCodeFirstContext : DbContext { public EFCodeFirstContext() : base("name=MyConnectionString") { //Setting the Database Initializer as DropCreateDatabaseIfModelChanges Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFCodeFirstContext>()); } public DbSet<Student> Students { get; set; } } }
Please make sure to have the connection string with the name MyConnectionString within the app.config file or web.config file as shown in the below image.
Now, modify the Main method of the Program class as follows. Here, we are adding one Student Entity to the database. There is no validation of the length of the data we are storing in the properties.
using System; namespace EFCodeFirstDemo { class Program { static void Main(string[] args) { using (EFCodeFirstContext context = new EFCodeFirstContext()) { var student = new Student() { FirstName = "Pranaya", LastName = "Rout" }; context.Students.Add(student); context.SaveChanges(); Console.WriteLine("Student Added"); Console.ReadKey(); } } } }
With the above changes in place, now run the application and verify the database. Here, you can see, it created the corresponding database columns with the maximum size.
How to Set the Max Length and Min Length in Entity Framework?
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. For this, we need to use the MaxLength(50) and MinLength(5) Attributes.
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 set the corresponding database column length as 50 as well as if we enter the FirstName value of more than 50 characters, then 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 the value less than 5 characters. For MinLength Attribute, Entity Framework will set the corresponding database column length as max.
using System.ComponentModel.DataAnnotations; namespace EFCodeFirstDemo { 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, now run the application and you should get the following exception. This makes sense as we have decorated the LastName property with MinLength(5) Attribute and we are trying to store a value in the LastName property with 4 characters.
Now, if you verify the database, then 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 below image.
Note: The point that you need to remember is Entity Framework validates the MaxLength and MinLength property values. For MaxLength, whatever value you specified that will be set as the corresponding database column size. And For MinLength, Entity Framework will set the corresponding database column size as max.
How can we set both MaxLength and MinLength Attribute in a single Property?
It is also possible to apply both MaxLength and MinLength Attribute in a single Property. 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 EFCodeFirstDemo { public class Student { public int StudentId { get; set; } [MaxLength(10), MinLength(5)] public string FirstName { get; set; } public string LastName { get; set; } } }
Now, modify the Main method as shown below. Here, we are trying to enter more than 10 characters in the FirstName property of the Student Entity.
using System; namespace EFCodeFirstDemo { class Program { static void Main(string[] args) { using (EFCodeFirstContext context = new EFCodeFirstContext()) { var student = new Student() { FirstName = "PranayaKumarRout", LastName = "Rout" }; context.Students.Add(student); context.SaveChanges(); Console.WriteLine("Student Added"); Console.ReadKey(); } } } }
Now, when you run the application, you should get the following validation exception.
In the next article, I am going to discuss StringLength Attribute in Entity Framework Code First Approach with Examples. Here, in this article, I try to explain MaxLength and MinLength Data Annotation Attribute in Entity Framework Code First Approach with Examples. I hope you enjoyed this MaxLength and MinLength Attribute in Entity Framework Code First Approach with Examples article. Please give your valuable feedback and suggestions about this article.